zoukankan      html  css  js  c++  java
  • URAL 1161 Stripies(数学+贪心)

    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):
    1 from math import sqrt
    2 n = input()
    3 a = []
    4 for _ in xrange(n):
    5     a.append(input())
    6 a.sort(reverse = True)
    7 for i in xrange(1, n):
    8     a[i] = 2 * sqrt(a[i] * a[i - 1])
    9 print "%.2f" % a[-1]
    View Code
  • 相关阅读:
    微服务架构有哪些优势?
    Java 线程数过多会造成什么异常?
    Java 死锁以及如何避免?
    抽象的(abstract)方法是否可同时是静态的(static), 是否可同时是本地方法(native),是否可同时被 synchronized 修饰?
    内部类可以引用它的包含类(外部类)的成员吗?有没有 什么限制?
    CSS选取第几个标签元素:nth-child、first-child、last-child
    数据库约束
    DQL查询语句
    网络编程(客户端,服务端文件上传下载)
    缓冲流,转换流
  • 原文地址:https://www.cnblogs.com/oyking/p/3577262.html
Copyright © 2011-2022 走看看