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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章