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\n",bacts.top());
    }
    return 0;
}

转载于:https://www.cnblogs.com/DrCarlluo/p/6580611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值