集火脆皮(贪心算法)

题目是哪的我给忘了。

大意要求输入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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值