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 50Sample 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);}}
相关文章推荐
- HTML5学习笔记简明版(4):新元素之video,audio,meter,datalist,keygen,output
- 图片切割方法
- placeholder
- 求多个数的最小公倍数的问题
- Using CSV-Format Log Output
- 什么是向量中断,什么是中断向量?
- Excel中随心所欲显示日期的格式
- Difference between Enumeration and Iterator in Java
- LAMP服务器环境之编译(部分)安装-php的modules模式
- makefile 编写之32 or 64位机器
- EF code first attribute
- springmvc + hibernate构建web工程(二)
- 为qt添加sql驱动
- SQL Server 海量数据查询代码优化以及建议
- 创建 AngularJS 自定义过滤器,带自定义参数
- Android开发之ListView-BaseAdapter的使用
- Python简单调用MySQL存储过程并获得返回值的方法
- GRE写作必备句型
- Snail—OC学习之Hello,World!
- 基于opencv的一种局部自适应快速二值化方法(积分法)