HDU4310贪心

题意:打Dota, 你的队友死了,就你一个人,你需要1Vn,你的攻击只有一点,也就是你打敌方hero一下只能打掉他一滴血,但是你的hp是无限的,打斗是回合制:你打他们中的一个一下,他们所有的英雄打你一下(你掉的血就是他们还活着的hero的总攻击)。贪心思想,你会发现,先干掉 攻击/血量 最大的敌方hero便是本题的贪心策略,所以,对 攻击/血量 降序排序,一个一个的干掉,贴代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

typedef struct node
{
    int hp, dps;
};

node hero[100];

int cmp(node a, node b)
{
    return 1.0*a.dps/a.hp > 1.0*b.dps/b.hp;
}

int main()
{
    int n, sumdps = 0, sumhp = 0;

    while(~scanf("%d", &n))
    {
        sumdps = sumhp = 0;
        for(int i=0; i<n; ++i)
        {
            scanf("%d%d", &hero[i].dps, &hero[i].hp);
            sumdps += hero[i].dps;
        }

        sort(hero, hero+n, cmp);

        for(int i=0; i<n; ++i)
        {
            sumhp += sumdps*hero[i].hp;
            sumdps -= hero[i].dps;
        }

        printf("%d\n", sumhp);
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值