zoukankan      html  css  js  c++  java
  • hdu4310

    2017-08-26  15:25:22

    writer:pprp

    题意描述:

    • 1 VS n对战,回合制(你打他们一下,需要受到他们所有存活人的
    攻击)
    • 你的血量无上限,攻击力为1
    • 对手血量及攻击力给定
    • 消灭所有敌人掉最少的血量
    • n ≤ 20

    贪心的去做,应该优先解决那些攻击力高血量低的敌人,所以应该按照 攻击力/血量 降序排列然后处理就好了

    代码如下:

    /*
    @theme:hdu 4310
    @writer:pprp
    @declare:简单的贪心算法 将攻击力/血量最高的敌人先进攻下来就行了
    @date:2017/8/26
    */
    #include <bits/stdc++.h>
    
    using namespace std;
    class enemy
    {
    public:
        double dps;
        double hp;
    } emy[1010];
    
    
    struct cmp
    {
        bool operator()(const enemy& a, const enemy&b)
        {
            return a.dps/a.hp > b.dps/b.hp;
        }
    };
    
    int main()
    {
        int n;
    
        while(cin >> n && n >= 1 && n <= 20)
        {
            double ans = 0;
            double sum_dps = 0;
            for(int i = 0 ; i < n ; i++)
            {
                cin >> emy[i].dps >> emy[i].hp;
                sum_dps += emy[i].dps;
            }
    
            sort(emy, emy + n,cmp());
    
    //        for(int i = 0 ; i < n ;i++)
    //        {
    //              cout << emy[i].dps << " " << emy[i].hp << endl;
    //        }
    
            for(int i = 0 ; i < n ; i++)
            {
                if(i == 0)
                {
                    ans += emy[0].hp * sum_dps;
                }
                else
                {
                    sum_dps -= emy[i-1].dps;
                    ans += emy[i].hp * sum_dps;
                }
            }
    
            cout << ans << endl;
        }
        return 0;
    }
  • 相关阅读:
    Groovy Simple file download from URL
    Connect to URL and dump webpage in Groovy Stack Overflow
    bash脚本测网络流量
    HTTPBuilder Overview
    nginx设置代理账号密码
    引爆流行
    RRDtool About RRDtool
    老王 python ,
    Ubuntu下安装GeoIP
    实时查看网络的流量
  • 原文地址:https://www.cnblogs.com/pprp/p/7435444.html
Copyright © 2011-2022 走看看