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. docker confluence

    http://wuyijun.cn/shi-yong-dockerfang-shi-an-zhuang-he-yun-xing-confluence/ https://hub.docker.com/r ...

  2. AutoLayout技术选型和应用

    前言:这篇文章是笔者在项目中对布局技术进行技术选型和应用的相关介绍,供大家参考. && [self.buttonscount] > 0) { UIButton *button = ...

  3. VB的判断语句和循环语句

      判断语句 •If语句 if语句共有4种写法: 第一种语法: If 条件判断语句 then 程序代码 第二种语法:If 条件判断语句 then 程序代码 else 程式代码 第三种语法: If 条件 ...

  4. Asp.net MVC 版本简史

    http://www.dotnet-tricks.com/Tutorial/mvc/XWX7210713-A-brief-history-of-Asp.Net-MVC-framework.html A ...

  5. C#访问url地址并返回数据

    public partial class Form1 : Form { static bool isSelect = false; public Form1() { InitializeCompone ...

  6. 帝国CMS备忘

    一. 2级导航: 类似下图这种导航: 实现方式如: 1. 定义一个标签模板,记住ID,具体内容如: 页面模板内容: <li><a href=”[!—bclassurl—]”>[ ...

  7. [LeetCode]题解(python):035-Search Insert Position

    题目来源 https://leetcode.com/problems/search-insert-position/ Given a sorted array and a target value, ...

  8. Naming Conventions for .NET / C# Projects

    http://www.akadia.com/services/naming_conventions.html Naming Conventions for .NET / C# Projects Mar ...

  9. C++经典编程题#5:寻找下标

    总时间限制:  1000ms 内存限制:  65536kB 描述 已知一个整数数组x[],其中的元素彼此都不相同.找出给定的数组中是否有一个元素满足x[i]=i的关系,数组下标从0开始.     举例 ...

  10. 我的工具箱之Opera浏览器

    下载地址:http://pan.baidu.com/s/1gdVQA11 刚出来时挺火的,后来有点渐趋式微了.