题目链接:http://poj.org/problem?id=1862
CSUST 2012年8月19日
暑假组队后第10次个人训练赛http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=11732#problem/B
POJ 1862 Stripies
Time Limit: 1000MS |
Memory Limit: 30000K |
|
Total Submissions: 9477 |
Accepted: 4610 |
Description
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
3
72
30
50
Sample Output
120.000
Source
Northeastern Europe 2001, Northern Subregion
题意:即给你一定的生物,他们会有一定的重量,如果他们相互碰撞, 那么根据题目给的那个公式2*sqrt(m1*m2) ,质量会减少。
这个公式表示的是两个物品质量分别为m1和m2,而他们碰撞后的总质量会减少为2*sqrt(m1*m2)
给你一定的这样的生物及它们的质量,要你求它们经过碰撞后的最小总质量。
容易看出,所有的生物全部相撞了后,肯定可以得到一个总质量
但是如果排列碰撞的顺序,使得最后的总质量最小呢???
由于数学没有学好,本人只好手动测试。。。。。。
给你三个这样的生物,质量分别为a,b,c
m= 2*sqrt(2*sqrt(a*b)*c)
经过测试,(有兴趣的同学可以自己证明下。。。然后告诉我)
要使得m最小,那么c一定是三个数中最小的一个。
解法:
然后题目就简单啦。直接一个sort排序,把给的生物由大到小排序就O了,然后从大一路撞到小,O(∩_∩)O哈哈~撞来撞去很好玩吧。注意sort要使用的头文件哈
#include<algorithm>
using namespace std;
注意:(感谢kb神的提醒)n=1的情况不用撞,直接输出结果就行了,比赛时没注意这个问题,一路WR到死啊TOT
//AC C++ 184kb 32ms/0ms B - Stripies POJ 1862 //感谢kb神的提醒。。。。。。 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int main() { int n; int i; double a[110]; double ans; while(scanf("%d",&n)!=EOF) { ans=0; for(i=0;i<n;i++) { scanf("%lf",&a[i]); ans+=a[i]; } sort(a,a+n); if(n>1)//不要忘了处理n==1情况,否则一路WA到底啊 { ans=a[n-1]; for(i=n-2;i>=0;i--) { ans=2*sqrt(a[i]*ans); } } printf("%.3lf\n",ans); } return 0; }