1poj1862(贪心)

来源:互联网 发布:win10关闭软件快捷键 编辑:程序博客网 时间:2024/05/29 09:08

http://poj.org/problem?id=1862

Stripies
Time Limit: 1000MSMemory Limit: 30000KTotal Submissions: 9472Accepted: 4608

Description

Our chemical biologists haveinvented a new very useful form of life called stripies (in fact,they were first called in Russian - polosatiki, but the scientistshad to invent an English name to apply for an internationalpatent). The stripies are transparent amorphous amebiform creaturesthat live in flat colonies in a jelly-like nutrient medium. Most ofthe time the stripies are moving. When two of them collide a newstripie appears instead of them. Long observations made by ourscientists enabled them to establish that the weight of the newstripie isn't equal to the sum of weights of two disappearedstripies that collided; nevertheless, they soon learned that whentwo stripies of weights m1 and m2 collide the weight of resultingstripie equals to 2*sqrt(m1*m2). Our chemical biologists are veryanxious to know to what limits can decrease the total weight of agiven colony of stripies.
You are to write a program that will help them to answer thisquestion. You may assume that 3 or more stipies never collidetogether.

Input

The first line of the inputcontains one integer N (1 <= N <=100) - the number of stripies in a colony. Each of next N linescontains one integer ranging from 1 to 10000 - the weight of thecorresponding stripie.

Output

The output must contain one linewith the minimal possible total weight of colony with the accuracyof three decimal digits after the point.

Sample Input

3723050

Sample Output

120.000

Source

Northeastern Europe 2001, Northern Subregion
题意:已知n个数,一次两个按照2*sqrt(m1*m2)来结合,输出最小的结果

//思路:关键在于证明任意三个数如何结合,结果才能最小

假设有a,b,c 且结果是r
则 r = 2*sqrt(2*sqrt(a*b)*c)
则 r^2/8 = sqrt(a*b*c*c);
若要 r 最小 则 c 一定是 a,b,c 中最小的 所以就是不断地取两个大数相乘喽~~
我用测试数据计算了下发现每次发最大拿来计算就行了。

#include<iostream>
#include<algorithm>
#include<cmath>
double a[110];
using namespace std;
int main()
{
 int n;
 while(scanf("%d",&n)!=EOF)
 {
  memset(a,0,sizeof(a));
  int i;
  for(i=0;i<n;i++)
   scanf("%lf",&a[i]);
  sort(a,a+n);
  for(i=n;i>=2;i--)
  {
   a[i-2]=2*sqrt(a[i-1]*a[i-2]);
  }
  printf("%.3lf\n",a[0]);
 }
 return 0;
}

原创粉丝点击