Hdu - 4310 - Hero

先上题目

Hero

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2131    Accepted Submission(s): 960


Problem Description
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
 
 
  题意很简单,就是你有一只DPS为1,HP无限的英雄,对方阵营有n只英雄,且他们的DPS和HP给定,问要把对方的英雄全杀完,自己至少要扣多少血。
  排位赛的时候看到很快就有人过了这题,然后就开始做,看完题后目测是贪心,于是就开始想贪心的策略是什么,然后进行了证明,证明如下:
  假如对方有两只英雄,他们的血和秒伤分别是H1,H2,D1,D2然后列出先杀其中一只的两种情况,得到式子,然后相减,得出H1D2-H2D1(也有可能调转),然后就可以得出当杀某一只英雄的时候扣血少一点的条件,然后根据这个条件对这些英雄排序,逐只杀掉,就可以得到最后的结果。
  排序的时候一开始我用的是H/D 还是D/H,结果wa了,后来用H1D2和H2D1来判断就过了,不过其他人好像是用相除的方法来判断,也过了。
 
上代码:
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #define MAX 30
 5 #define LL long long
 6 using namespace std;
 7 
 8 typedef struct
 9 {
10     LL d;
11     LL h;
12 }hero;
13 
14 hero e[MAX];
15 
16 bool cmp(hero x,hero y)
17 {
18     LL D=x.d*y.h-y.d*x.h;
19     if(D>0) return 1;
20     else if(D==0 && x.d>y.d) return 1;
21     else if(D==0 && x.d==y.d && x.h<y.h) return 1;
22     return 0;
23 }
24 
25 int main()
26 {
27     LL n,i,tot,sum;
28     //freopen("data.txt","r",stdin);
29     while(scanf("%lld",&n)!=EOF)
30     {
31         for(i=0;i<n;i++) {scanf("%lld %lld",&e[i].d,&e[i].h);}
32         sort(e,e+n,cmp);
33         tot=0;
34         sum=0;
35         for(i=0;i<n;i++)
36         {
37             tot+=e[i].h;
38             sum+=tot*e[i].d;
39         }
40         printf("%lld\n",sum);
41     }
42     return 0;
43 }
4310

 

posted @ 2013-08-01 19:10  海拉鲁的林克  阅读(488)  评论(0编辑  收藏  举报