集火脆皮(贪心算法)

来源:互联网 发布:金淘店管软件靠谱吗 编辑:程序博客网 时间:2024/04/28 16:15

题目是哪的我给忘了。

大意要求输入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;}


0 0