Loading

[HDU - 4310]Hero

典型的打怪兽贪心。

题目讲的是在你打游戏时面对了以一打多的情况,这时你开了无敌状态,有无限的生命,但是你的攻击力变为了1,试问你把所有敌人打倒时你消耗的最少生命值。
将DFS与HP的比值进行从大到小的排序,然后按这个顺序依次打。

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct st
{
    double DPS;
    double HP;
}hero[50];

int cmp(st a,st b)
{
    return a.DPS/a.HP>b.DPS/b.HP;
}

int main()
{
    int n,i;
    long long sum,total;

    while(scanf("%d",&n)!=EOF)
    {
        sum=0;total=0;
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf",&hero[i].DPS,&hero[i].HP);
            total+=hero[i].DPS;
        }
        
        sort(hero,hero+n,cmp);
        
        for(i=0;i<n;i++)
        {
            sum+=hero[i].HP*total;
            total-=hero[i].DPS;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

 

posted @ 2019-08-20 14:11  ViKyanite  阅读(133)  评论(0编辑  收藏  举报