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(m1*m2). 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.
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.
The first line of the input 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.
The output must contain one line with the minimal possible total weight of colony with the accuracy of three decimal digits after the point.
3
72
30
50
120.000
题意:从N个数任取两个数按2*sqrt(a*b)合成新数放回,求最后那个数的最小值。
题解:
值大的优先
证: 假设三个数a, b, c
先乘a,b再乘c: 2*sqrt( 2*sqrt( a*b ) * c ) == 2*sqrt( 2*sqrt( a*b*c ) * sqrt(c) )
先乘a,c再乘b: 2*sqrt( 2*sqrt( a*c ) * b ) == 2*sqrt( 2*sqrt( a*b*c ) * sqrt(b) )
先乘b,c再乘a: 2*sqrt( 2*sqrt( b*c ) * a ) == 2*sqrt( 2*sqrt( a*b*c ) * sqrt(a) )
只由最后的sqrt(a), sqrt(b), sqrt(c)决定大小
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
priority_queue< double >q;
int n;
double a, b;
int main() {
scanf( "%d", &n );
for( int i=1; i<=n; i++ ) {
scanf( "%lf", &a );
q.push(a);
}
for( int i=1; i<n; i++ ) {
a=q.top(); q.pop();
b=q.top(); q.pop();
q.push( sqrt( a*b )*2 );
}
printf( "%.3lf\n", q.top() );
return 0;
}