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, 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

【代码】:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;

#define LL long long
using namespace std;
const int N = 20000+100;
int n;
double L;
double a,b;
priority_queue<double> q;
int main()
{
    cin>>n;

        for(int i=0;i<n;i++)
        {
            cin>>L;
            q.push(L);
        }
        while(q.size()>1)
        {
            a=q.top();q.pop();

            b=q.top();q.pop();

            q.push(2*sqrt(a*b));
        }
    printf("%.3f\n",q.top());

    return 0;
}

时间: 2024-11-06 03:38:22

POJ 1862 Stripies【哈夫曼/贪心/优先队列】的相关文章

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;

poj3253 Fence Repair【哈夫曼树+优先队列】

Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a s

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 <

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 【优先队列】

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

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

一道贪心的水题,读完题目,直接把样例的三个数试一试,就知道怎么一种组合方式会产生最小的结果. (让我想起了哈弗曼编码,用了优先队列) 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 3253 Fence Repair 类似哈夫曼树的贪心思想

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24550   Accepted: 7878 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)

poj 3253 Fence Repair(优先队列+哈夫曼树)

题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每一个父节点都是两个子节点的和.这个题就是可以从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个元素为止. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <cty