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. 低功耗蓝牙4.0BLE编程-nrf51822开发(6)-Battery Service

    Battery Service是有关电池特性方面的服务,如果需要它,在初始化时将它加入到蓝牙协议栈. 如果通过ble_bas_battery_level_update(),电池电量将会通知,Batte ...

  2. Apache的HBase与cdh的sqoop集成(不建议不同版本之间的集成)

    1.修改sqoop的配资文件 2.从mysql导入到hbase(import) bin/sqoop import \ --connect jdbc:mysql://linux-hadoop3.ibei ...

  3. MVVM with ReactiveCocoa

    内容提要: 本文首先对比MVC简单介绍了MVVM的概念和优点,其次,简单介绍了Reactive Cocoa的使用,最后,通过一个例子介绍了使用Reactive Cocoa的MVVM框架. 正文: 首先 ...

  4. iOS _BSMachError: (os/kern) invalid capability (20)

    _BSMachError: (os/kern) invalid capability (20) 解决办法:将info.plist里面的en改为United States 2016-04-18 22:4 ...

  5. LeetCode Longest Palindrome

    原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...

  6. 玩转HTML5移动页面

    (1) 动画雪碧图涉及的动画十分多,用的元素也十分多,请务必使用雪碧图(Sprite)!网上的工具有一些可以帮助你生成雪碧图的工具,例如CssGaga,GoPng等等,自动化构建工具Grunt和Gul ...

  7. DBCC TRACEON/TRACEOFF/TRACESTATUS

    1. enable trace DBCC TRACEON ( trace# [ ,...n ][ , -1 ] ) [ WITH NO_INFOMSGS ] trace# Is the number ...

  8. iOS:特殊符号大全

    特殊符号大全分享给大家,直接复制粘贴就可以使用了! ░ ▒ ▬ ♦ ◊ ◦ ♠ ♣ ▣ ۰•● ❤ ●•۰ ► ◄ ▧ ▨ ♨ ◐ ◑ ↔ ↕ ▪ ▫ ☼ ♦ ♧♡♂♀♠♣♥❤☜☞☎☏⊙◎ ☺☻☼▧▨ ...

  9. docker-compose常用命令

    --verbose:输出详细信息-f 制定一个非docker-compose.yml命名的yaml文件-p 设置一个项目名称(默认是directory名)docker-compose的动作包括:bui ...

  10. jenkins邮件模板

    步骤 1.在jenkins主目录中新建一个模板文件夹 命名为:email-templates 3.把模板代码放入到模板文件夹  with_results.groovy 4.设置邮件发送模板配置 5.配 ...