POJ 1862 Stripies 【贪心】

题目链接

题意

有一种生物,他们两两融合过后的质量是原来的几何平均数的二倍,求所有的融合之后能够得到的质量最小值。

分析

主要是要思考到如何贪心,结论是:不断让所有生物中质量最大的两个进行融合,直到只剩一个位置(即使答案)

证明:

n个生物,他们的质量分别是m1,m2,,mn,则它们融合过后的质量为

M=222m1m2m3mn=21+12++12n1m12n11m12n12m12n

可见,这种方法可以让质量更大的进行更多次的开方,因此得到的是最小值。
而实现起来也十分简单,维护优先队列即可。

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <list>
#include <sstream>
#include <stack>
using namespace std;

#define cls(x) memset(x,0,sizeof x)
#define inf(x) memset(x,0x3f,sizeof x)
#define neg(x) memset(x,-1,sizeof x)
#define ninf(x) memset(x,0xc0,sizeof x)
#define st0(x) memset(x,false,sizeof x)
#define st1(x) memset(x,true,sizeof x)
#define INF 0x3f3f3f3f
#define lowbit(x) x&(-x)
#define bug cout<<"here"<<endl;
//#define debug

priority_queue<double> bacts;

void cal()
{
    double a=bacts.top();
    bacts.pop();
    double b=bacts.top();
    bacts.pop();
    bacts.push(2*sqrt(a*b));
    return;
}

int main()
{
    #ifdef debug
        freopen("E:\Documents\code\input.txt","r",stdin);
        freopen("E:\Documents\code\output.txt","w",stdout);
    #endif
    int N=0;
    int a;
    while(scanf("%d",&N)!=EOF)
    {
        while(bacts.size())
            bacts.pop();
        while(N--)
        {
            scanf("%d",&a);
            bacts.push(a);
        }
        while(bacts.size()>1)
            cal();
        printf("%.3lf
",bacts.top());
    }
    return 0;
}
原文地址:https://www.cnblogs.com/DrCarlluo/p/6580611.html