POJ1862 Stripies 贪心 B

时间:2024-10-03 19:06:38

POJ 1862 Stripies

https://vjudge.net/problem/POJ-1862

题目:

    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. 

Input

    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.

Output

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

Sample Input


Sample Output

120.000

分析:

贪心题目

题目本身不难,总共就几种策略,WA几发蒙也能蒙过了

问题在于这个题为什么这么做是正确的

我在做的时候就顺便用txt进行了证明,毕竟是练习,也为了保证一遍过

POJ1862 Stripies 贪心 B

首先写了这个,然后意识到不是相加,是直接把v1和v2变成另外一种,所以这是错误的

POJ1862 Stripies 贪心 B

然后列了几种情况,当两个数的时候,产生的结果是不固定的,可能大,可能小,也可能与其中一个相等,这里就可以想到用增加变量然后用字母来代替数值进行分析

POJ1862 Stripies 贪心 B

V1v2v3

然后进行第一步,如果12合并,如果23合并,如果13合并

POJ1862 Stripies 贪心 B

一行一个情况

在这其中保证

POJ1862 Stripies 贪心 B

那么最后的三个结果就是

POJ1862 Stripies 贪心 B

看起来比较复杂emmmm

那就简单化简吧,假设第一个和第二个相等

POJ1862 Stripies 贪心 B

化简

POJ1862 Stripies 贪心 B

然后取平方

POJ1862 Stripies 贪心 B

诶是不是发现什么了

都会有

POJ1862 Stripies 贪心 B

这个东西,

唯一的区别是v1,v2,v3

那么显然是

POJ1862 Stripies 贪心 B

倒推:

POJ1862 Stripies 贪心 B

得证

放全部分析过程:

POJ1862 Stripies 贪心 B

v1+v2 -> 2*sqrt(v1*v2)

sqrt(v1^2+v2^2+2*v1*v2) -> sqrt(4*v1*v2)

1 100

20

72 50

120

2 50

20

5 20

20

v1 v2 v3

2*sqrt(v1*v2) v3

2*sqrt(v1*v3) v2

2*sqrt(v2*v3) v1

v1<v2<v3

2*sqrt(2*sqrt(v1*v2)*v3)

2*sqrt(2*sqrt(v1*v3)*v2)

2*sqrt(2*sqrt(v2*v3)*v1)

2*sqrt(2*sqrt(v1*v2)*v3)=2*sqrt(2*sqrt(v1*v3)*v2)

sqrt(v1*v3)*v2=sqrt(v1*v2)*v3

v1*v2*v3*v3  v1*v3*v2*v2

v1*v2*v3

v1*v2*v3*v1 min

也就是sqrt(v2*v3)*v1

再倒推就是2*sqrt(2*sqrt(v2*v3)*v1)

再倒推就是先处理两个大的

得证

AC代码:

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <time.h>
#include <queue>
#include <string.h>
#define sf scanf
#define pf printf
#define lf double
#define ll long long
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
#define LL long long
#define eps 1e-6
using namespace std;
double a[];
bool cmp(double a, double b){
return a > b;
}
int main() {
int n = ;
s(n);
for(int i = ; i < n; i ++){
slf(a[i]);
}
sort(a,a+n,cmp);
double temp = a[];
for(int i = ; i< n ;i ++){
temp = 2.0*sqrt(a[i]*temp);
}
pf("%.3lf\n",temp);
return ;
}