Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian - polosatiki, but the scientists had to invent an English name to apply for an international patent). The stripies are transparent amorphous amebiform creatures that live in flat colonies in a jelly-like nutrient medium. Most of the time the stripies are moving. When two of them collide a new stripie appears instead of them. Long observations made by our scientists enabled them to establish that the weight of the new stripie isn't equal to the sum of weights of two disappeared stripies that collided; nevertheless, they soon learned that when two stripies of weights m1 and m2 collide the weight of resulting stripie equals to 2·sqrt(m1m2). Our chemical biologists are very anxious to know to what limits can decrease the total weight of a given colony of stripies.
You are to write a program that will help them to answer this question. You may assume that 3 or more stipies never collide together.

Input

The first line contains one integer N (1 ≤ N ≤ 100) - the number of stripies in a colony. Each of next N lines contains one integer ranging from 1 to 10000 - the weight of the corresponding stripie.

Output

The output must contain one line with the minimal possible total weight of colony with the accuracy of two decimal digits after the point.
 
题目大意:给你n个正整数,结合两个数a、b可以得到2 * sqrt(a * b),问结合这n个数可以得到的最小的数是多少。
思路:1个2个的不用考虑了
先考虑3个数的情况,比如a、b、c,计算可有

2 * sqrt(a * 2 * sqrt(b * c))
= 2 * 2^(1/2) * a^(1/2) * b^(1/4) * c^(1/4)

显然当a最小的时候,这个式子的答案最小,代入可得样例的答案。

可以想象,对于上面的式子来说,最大的数,开方数肯定是最小的才是最好的(如上面的1/4),所以当忽略前面的常数(就是那些2)的时候,肯定是先合并最大的两个。

合并完之后,剩下n-1个数,最新得到的数肯定是最大的(假设a<b,那么sqrt(a * b)>sqrt(a*a)>a)

然后,肯定还是合并最大的两个,即剩下的n-2个和最新得到的数。如此重复。

那么,算上前面的常数呢?上面的算法得到的数是形如2 * 2^(1/2) * 2^(1/4) * …… * 2^(1/2)^(n-1) * a[1]^(1/2) * a[2]^(1/4) * …… * a[n]^(n-1)的式子。

其常数2 * 2^(1/2) * 2^(1/4) * …… * 2^(1/2)^(n-1)一定也是所有结合中最小的,因为把这些数的指数从大到小排列的话,第 i 个数的指数一定不会小于2^(1/2)^(i-1)(想想合并的时候,一定有一个2,那么这些2的指数的指数,连在一起的相差一定不会超过1),然而我们已经取到最小的了。

那么,前后都是最小的,合起来也肯定是最小的。

所以。解法就是,排序,然后两两结合。输出结果。

 
代码(0.062S):
 from math import sqrt
n = input()
a = []
for _ in xrange(n):
a.append(input())
a.sort(reverse = True)
for i in xrange(1, n):
a[i] = 2 * sqrt(a[i] * a[i - 1])
print "%.2f" % a[-1]

URAL 1161 Stripies(数学+贪心)的更多相关文章

  1. POJ 1862 & ZOJ 1543 Stripies(贪心 | 优先队列)

    题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  2. 【POJ】1862:Stripies【贪心】【优先队列】

    Stripies Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20456   Accepted: 9098 Descrip ...

  3. 【POJ - 1862】Stripies (贪心)

    Stripies 直接上中文了 Descriptions 我们的化学生物学家发明了一种新的叫stripies非常神奇的生命.该stripies是透明的无定形变形虫似的生物,生活在果冻状的营养培养基平板 ...

  4. 洛谷3月月赛div2 题解(模拟+数学+贪心+数学)

    由于本人太蒻了,div1的没有参加,胡乱写了写div2的代码就赶过来了. T1 苏联人 题目背景 题目名称是吸引你点进来的. 这是一道正常的题,和苏联没有任何关系. 题目描述 你在打 EE Round ...

  5. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  6. ural 1303 Minimal Coverage(贪心)

    链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 按照贪心的思想,每次找到覆盖要求区间左端点时,右端点最大的线段,然后把要求覆盖的区间 ...

  7. HDOJ 5073 Galaxy 数学 贪心

    贪心: 保存连续的n-k个数,求最小的一段方差... .预处理O1算期望. .. Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  8. FZU 2144 Shooting Game(数学+贪心)

    主要思路:求出蚊子到达球的时间区间(用方程得解),对区间做一个贪心的选择,选择尽可能多的区间有交集的区间段(结构体排序即可),然后计数. #include <cstdio> #includ ...

  9. hdu 3573(数学+贪心)

    Buy Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. phpcms v9模版调用代码大全(全面而实用)

    首页调用栏目 {pc:content action="category" siteid="$siteid" num="15" order=& ...

  2. 0011 SDK测试方法&ADB语法

    h1.移动云OSS SDK测试方法,包含环境搭建 android  oss sdk 测试方法 1.          搭建测试环境 1.     A ndroid模拟器需要jdk1.6 2.      ...

  3. 【Android测试】【第九节】MonkeyRunner—— 初识

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4836815.html 不得不说两句,过了这么久才再次更新博 ...

  4. Intent Flag(转)

    转载自 http://blog.csdn.net/berber78/article/details/7278408 一. intent.setFlags()方法中的参数值含义: 1.FLAG_ACTI ...

  5. Dynamics AX Bitmap to Image File

    static void SSW_Bit2ImageFile(Args _args) { Bitmap curBitmap; Image curImage; ; curBitmap = FormLett ...

  6. 用css实现云状提示框

    经常会用到云状提示框,如图: 基本框架是这样,以三角在左侧为例: <div class="container"> <div class="content ...

  7. 【转】Android新组件Material Dialog,SwipeRefreshLayout,ListPopupWindow,PopupMenu等

    朝花夕拾----新组件的学习和使用 分类: Android UI2015-06-26 11:31 440人阅读 评论(0) 收藏 举报 uidialogMaterial   目录(?)[-] Mate ...

  8. Swift闭包

    把Swift中的 block 常见的声明和写法作一个总结.以免后续忘了,好查阅. // //  blockDemo.swift //  swiftDemo // //  Created by appl ...

  9. JQuery: 基本知识了解

    一.介绍:jQuery 是一个 JavaScript函数库.它极大地简化了 JavaScript 编程.jQuery 库可以通过一行简单的标记被添加到网页中.jQuery 是一个轻量级的"写 ...

  10. MySQL · 性能优化 · 条件下推到物化表

    MySQL · 性能优化 · 条件下推到物化表 http://mysql.taobao.org/monthly/2016/07/08/ 背景 MySQL引入了Materialization(物化)这一 ...