集火脆皮(貪心算法)

題目是哪的我給忘了。

大意要求輸入n組數,前一個數是DPS,後一個數是hp。每回合存活的敵人都會對你造成相當於DPS的傷害,你每回合都能對一個敵人造成一點傷害,要求求出所受的最少傷害。

思路就是先殺DPS/hp最大的敵人,如果相同就殺血少的。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
using namespace std;
struct node
{
    int hp;
    int dps;
};
bool cmp(node a, node b)
{
    if(1.0*a.dps/a.hp!=1.0*b.dps/b.hp)
        return 1.0*a.dps/a.hp>1.0*b.dps/b.hp;
    else return a.hp<b.hp;
}
int main()
{
    int n;
    while(cin>>n)
    {
        node e[22];
        int sumdps=0,sum=0;
        for(int i=0;i<n;i++)
        {
            cin>>e[i].dps>>e[i].hp;
            sumdps+=e[i].dps;
        }
        sort(e,e+n,cmp);
        for(int i=0;i<n;i++)
        {
            e[i].hp--;
            sum+=sumdps;
            if(e[i].hp==0)
                sumdps-=e[i].dps;
            else i--;
        }
        cout<<sum<<endl;
    }
    return 0;
}


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