您的位置:首页 > 其它

Stripies

2015-07-20 17:18 267 查看
DescriptionOur 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 transparentamorphous 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 establishthat 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). Ourchemical 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.InputThe 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.OutputThe 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
题目分析:题意要求是寻找最小的数,对输入数据进行排列,越大的数开根号所求得的数亏损越大,所以,对数据要从大到小进行讨论。每次讨论后都要把用过的数清除,然后得到的数插入再重新排列。1:贪心思想。代码如下:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>

using namespace std;

//bool comp(const int& a,const int& b)
//{
//    return a>b;
//}

int main()

{
int n;
double a[1000];
while(cin>>n)
{
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(int i=n-1;i>0;i--)
{
a[i-1]=2*sqrt(a[i]*a[i-1]);
sort(a,a+i-1);
}
//        cout<<a[0]<<endl;
printf("%.3f\n",a[0]);
}
return 0;
}
2:
优先队列
代码如下:
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<string>#include<queue>using namespace std;int main(){int n;double m;while(cin>>n){priority_queue<double>q;for(int i=0; i<n; i++){cin>>m;q.push(m);}double t,k,sum=0;while(q.size()>1){t=q.top();q.pop();k=q.top();q.pop();sum=2*sqrt(k*t);q.push(sum);}printf("%.3f\n",q.top());//        printf("%.3f\n",sum);}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: