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;
}
原文地址:https://www.cnblogs.com/pprp/p/7435444.html