zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    jsp页面input输入框限制输入内容
    sql计算两个日期之间的相差天数
    sql根据一个字段日期加减7天存入另一字段中
    ajax请求捕获异常
    跨网段和局域网的SQL SERVER发布订阅配置图解和常见问题
    一次得到多个数据集
    SQL Server 2008语句大全完整版
    关于已开票已收款未发货的账务处理
    高格-一些特点话题【7】
    高格-塑料管业中厂内工单,满足随时换料的处理【6】
  • 原文地址:https://www.cnblogs.com/DrCarlluo/p/6580611.html
Copyright © 2011-2022 走看看