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. FTS抓包看AVDTP

    1.概述   测试过程为打开Audio连接,没有听音乐,人后断开Audio连接,主要目的是为了测试AVDTP的工作流程.   2.Frame分析    首先贴出抓取的关于AVDTP的包: 在L2CAP ...

  2. 回调的代理(delegate)实现

    1.CoreManage.h #import <Foundation/Foundation.h> @protocol SampleProtocol; //声明核心类的属性和方法 @inte ...

  3. WCF两种方式

    http://www.ilanever.com/article/sitedetail.html?id=164 1. 显示添加服务行为,为服务自动提供基于HTTP-GET的元数据.2. 采用元数据交换终 ...

  4. MVC路由约束

    public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/ ...

  5. std::map的clear()没有用?

    昨天晚上,我徒弟跑过来讲,他的程序的内存占用居高不下,愿意是std::map的clear()没有效果.于是我让他用erase(begin,end); 试试也不行. 代码如下: void release ...

  6. <dependency>spring-webmvc</dependency>

    Spring 4.2.0.RELEASE版本: <dependency> <groupId>org.springframework</groupId> <ar ...

  7. ie8兼容border-radius方法

    <!doctype html><html> <head>        <meta charset="utf-8" />    &l ...

  8. jQuery学习之jQuery Ajax用法详解(转)

    [导读] jQuery Ajax在web应用开发中很常用,它主要包括有ajax,get,post,load,getscript等等这几种常用无刷新操作方法,下面我来给各位同学介绍介绍.我们先从最简单的 ...

  9. iOS面试题及答案2015.6.7

    iOS面试题及答案     1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答: Object-c的类不可以多重继承 ...

  10. 第三篇 Replication:事务复制-发布服务器

    本篇文章是SQL Server Replication系列的第三篇,详细内容请参考原文. 发布服务器是所有复制数据的源头.每一个发布服务器上可以定义多个发布.每一个发布包含一组项目(项目在同一个数据库 ...