HDU-4310-HERO

原题
When playing DotA with god-like rivals and pig-like team members, you have to face an embarrassing situation: All your teammates are killed, and you have to fight 1vN.

There are two key attributes for the heroes in the game, health point (HP) and damage per shot (DPS). Your hero has almost infinite HP, but only 1 DPS.

To simplify the problem, we assume the game is turn-based, but not real-time. In each round, you can choose one enemy hero to attack, and his HP will decrease by 1. While at the same time, all the lived enemy heroes will attack you, and your HP will decrease by the sum of their DPS. If one hero’s HP fall equal to (or below) zero, he will die after this round, and cannot attack you in the following rounds.

Although your hero is undefeated, you want to choose best strategy to kill all the enemy heroes with minimum HP loss.
Input
The first line of each test case contains the number of enemy heroes N (1 <= N <= 20). Then N lines followed, each contains two integers DPSi and HPi, which are the DPS and HP for each hero. (1 <= DPSi, HPi <= 1000)
Output
Output one line for each test, indicates the minimum HP loss.
Sample Input
1
10 2
2
100 1
1 100
Sample Output
20
201
题意:
你有n个敌人,每个人都有自己的攻击力d和生命值h,你每回合可以减少其中任何一个人的h 1点,当敌人的h<=0时死亡。但你每回合都要受到所有人的攻击,求你最少掉多少血。
题解:
根据计算可以把两个人之间的h1d2与h2d1进行比较,大的作为先攻击的对象。
计算过程:
不再赘述,读者可以在稿纸上列举两个例子t1和t2,分别计算先攻击t1和先攻击t2,中间所受伤害的差值,计算可以得出以上结论。
附上AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct enemy
{
    int d;//伤害值
    int h;//生命值
}a[25];
bool cmp(enemy a,enemy b)//以d.h作为关键值比较
{
    int c,d;
    c=a.h*b.d;
    d=a.d*b.h;
    return c<d;
}
int main()
{
    int n,k,z;
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,25*sizeof(enemy));//初始化
        k=0;
        z=0;
        for(int i=0;i<n;++i)
        {
            scanf("%d%d",&a[i].d,&a[i].h);
            z+=a[i].d;//计算最开始时的总伤害量
        }
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++)
        {
            k+=z*a[i].h;//计算伤害和
            z-=a[i].d;
        }
        printf("%d\n",k);
    }
    return 0;
}

以前写过的题,补发一下blog。
欢迎评论!

猜你喜欢

转载自blog.csdn.net/wjl_zyl_1314/article/details/82995726