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


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