POJ-1862-Stripies(贪心4)

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
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        double m,a[103];
        for(int i=0; i<n; i++)
        {
            scanf("%lf",&a[i]);
        }
        sort(a,a+n,cmp);
        for(int i=0;i<n-1;i++)
        {
           a[i+1] =2.0*sqrt(a[i]*a[i+1]);
        }
        printf("%.3lf\n",a[n-1]);//特别注意是输出最后一个 而不是a[i+1]
        //当n=1时,i+1就是0;
    }
    return 0;
}
时间: 2024-12-15 04:27:37

POJ-1862-Stripies(贪心4)的相关文章

POJ 1862 Stripies 贪心+优先队列

http://poj.org/problem?id=1862 题目大意: 有一种生物能两两合并,合并之前的重量分别为m1和m2,合并之后变为2*sqrt(m1*m2),现在给定n个这样的生物,求合并成一个的最小重量 思路: m1+m2 >=  2*sqrt(m1*m2) 所以每次取大的去合并,能变小. 直接优先队列就可以啦. #include<cstdio> #include<cmath> #include<queue> using namespace std;

POJ 1862 Stripies#贪心(水)

(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; int main() { int n; double w[105]; while(~scanf("%d",&n)) { for(int i=0;i<n;i++) { int m; scanf("%d",&

POJ 1862 Stripies 题解 《挑战程序设计竞赛》

题目:POJ - 1862 思路:每次挑选最大的两个数,进行2*sqrt(a,b)运算后放入到队列中.有点类似于之前做的fence repair题目. 这样可以保证大数被开方的次数更多,最后的结果更小. 注意: n为1的情况,WA了几次. 学会C++输出格式控制: http://c.biancheng.net/cpp/biancheng/view/2227.html. 1 #include <iostream> 2 #include <stdio.h> 3 #include <

POJ 1862 Stripies【哈夫曼/贪心/优先队列】

Stripies Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18198   Accepted: 8175 Description Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian - polosatiki, bu

9 POJ 1862 Stripies 简单贪心

观察发现m1+m2变为2*sqrt(m1*m2)质量是能够减少的, 因此按质量从大到小排序,每次取最大质量的两个合并,减少的质量是最多的. 合并n-1次,最终得到的一个数就是结果. 这里用优先队列写的比较方便. #include<cstdio> #include<queue> #include<cmath> using namespace std; priority_queue<double> q; int main() { int n,i; double

POJ 1862 - Stripies

一道贪心的水题,读完题目,直接把样例的三个数试一试,就知道怎么一种组合方式会产生最小的结果. (让我想起了哈弗曼编码,用了优先队列) 1 #include<cstdio> 2 #include<queue> 3 #include<cmath> 4 using namespace std; 5 int n; 6 int main() 7 { 8 while(scanf("%d",&n)!=EOF) 9 { 10 priority_queue&l

POJ 1862 Stripies 【优先队列】

题意:科学家发现一种奇怪的东西,他们有重量weight,如果他们碰在一起,总重变成2*sqrt(m1*m2).要求出最终的重量的最小值. 思路:每次选取质量m最大的两个stripy进行碰撞结合,能够得到最小的质量.所有只要维护一个优先队列就可以了 #include <iostream> #include <cstdio> #include <queue> #include <math.h> #include <cstring> #include

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]

poj 2431 Expedition 贪心+最大堆

当油量不够时从走过的油站中选最大加油量的 #include<iostream> #include<queue> #include<stdlib.h> #include<algorithm> using namespace std; #define MAX_N 10005 struct node{ int dist,fuel; }t[MAX_N]; bool cmp(const node &a,const node &b) { return a

POJ 1862 &amp; ZOJ 1543 Stripies(贪心 | 优先队列)

题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=543 Description Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian -