HDU 4310 贪心算法 C++版

Problem - 4310 (dingbacode.com)https://acm.dingbacode.com/showproblem.php?pid=4310当然是先选择攻击值最高的打了,一开始这么想,但是错了,因为攻击值高的可能血量也很高,很难打败。

所以,应该按照Dps/Hp从高到低的顺序开始攻击,Dps/Hp值越大,说明能最快速的削弱其战斗力,也说明排在前面的是攻击值相对较大,血量相对较少的。

然后在输入数据的时候,记录所有攻击值的总和,每打败一个,那么就在总和中减去它的攻击值。

#include <cstdio>
#include <algorithm>

using namespace std;

typedef struct hero {
    int dps;
    int hp;
    double hpAvg;

    bool operator<(const hero &a) const {
        return hpAvg > a.hpAvg;
    }
} hero;

int main() {
    int n;
    int totalDps;
    int totalHp;
    int loss;
    hero heros[22];
    while (scanf("%d", &n) != EOF) {
        totalDps = 0;
        totalHp = 0;
        for (int i = 0; i < n; ++i) {
            scanf("%d %d", &heros[i].dps, &heros[i].hp);
            heros[i].hpAvg = 1.0 * heros[i].dps / heros[i].hp;
            totalDps += heros[i].dps;
            totalHp += heros[i].hp;
        }
        sort(heros, heros + n);
        loss = 0;
        //击败n个英雄
        for (int i = 0; i < n; ++i) {
            int tempHp = heros[i].hp;
            while (tempHp > 0) {
                tempHp--;
                loss += totalDps;
            }
            totalDps -= heros[i].dps;
        }
        printf("%d\n", loss);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值