hdu 3979 Monster

来源:互联网 发布:深圳创意软件 编辑:程序博客网 时间:2024/06/15 19:41
Problem Description
One day, v11 encounters a group of monsters in a foreast. In order to defend the homeland, V11 picks up his weapon and fights!

All the monsters attack v11 at the same time. Every enemy has its HP, and attack value ATK. In this problem, v11 has his ATK and infinite HP. The damage (also means reduction for HP) is exactly the ATK the attacker has. For example, if v11's ATK is 13 and the monster's HP is 27, then after v11's attack, the monster's HP become 27 - 13 = 14 and vice versa.

v11 and the monsters attack each other at the same time and they could only attack one time per second. When the monster's HP is less or equal to 0 , we think this monster was killed, and obviously it would not attack any more. For example, v11's ATK is 10 and a monster's HP is 5, v11 attacks and then the monster is killed! However, a monster whose HP is 15 will be killed after v11 attack for two times. v11 will never stop until all the monsters are killed ! He wants to minimum the HP reduction for the fight! Please note that if in some second, some monster will soon be killed , the monster's attack will works too.
 

Input
The first line is one integer T indicates the number of the test cases. (T <=100)

Then for each case, The first line have two integers n (0<n<=10000), m (0<m<=100), indicates the number of the monsters and v11's ATK . The next n lines, each line has two integers hp (0<hp<=20), g(0<g<=1000) ,indicates the monster's HP and ATK.
 

Output
Output one line.

First output “Case #idx: ”, here idx is the case number count from 1. Then output the minimum HP reduction for v11 if he arrange his attack order optimal .
 

Sample Input
2 3 1 1 10 1 20 1 40 1 10 7 3
 

Sample Output
Case #1: 110 Case #2: 3
 
贪心:是下一篇color tree的铺垫(也是借鉴前人的忠告)
设怪物a,b 攻击分别为ja,jb,分别打t1,t2次能打死。
则先打a怪要满足:(ja+jb)*t1+jb*t2*jb<(ja+jb)*t2+ja*t1;化简可知如何选择最优(学习了,有时贪心的关键信息是隐蔽的(比如,需要打的次数));
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;typedef long long LL;struct f{    int hp;    int attack;    int num;}a[11000];bool cmp(f p,f q){ return p.attack*q.num>q.attack*p.num;}int main(){int t,cal=1;scanf("%d",&t);while(t--){    int n,m;    scanf("%d%d",&n,&m);      __int64 sum=0;    for(int i=0;i<n;i++){     scanf("%d%d",&a[i].hp,&a[i].attack);     if(a[i].hp%m==0) a[i].num=a[i].hp/m;     else a[i].num=a[i].hp/m+1;     sum+=a[i].attack;    }    sort(a,a+n,cmp);    __int64 s=0;  for(int i=0;i<n;i++)  {      s+=sum*a[i].num;      sum-=a[i].attack;  }  printf("Case #%d: %I64d\n",cal++,s);}return 0;}



0 0
原创粉丝点击