HDU 3979--monster
Monster
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1493 Accepted Submission(s):
383
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.
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.
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
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
自己的代码被WA了,看了别人的解题报告才知道,原来是思路出了点问题:
症结:(1) 我计算的是怪兽单位的血量所具有的攻击力,这样计算出来的时间是小数,但是打怪兽的时间必须都是整秒的,(这样就出现了问题)
别人的解题思路: 直接计算打死每只怪兽所需要的时间,然后按照单位时间内攻击力的大小排序,先打死单位时间内攻击力大的怪兽,(这才是正解)
自己被WA回来的代码:
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef struct node
{
long long hp;
long long atk;
double per;
}Node;
Node mt[10003];
bool cmp(Node a,Node b)
{
return a.per>b.per;
}
int main()
{
int t,kcase=0;;
cin>>t;
while(t--)
{
int n,m;//m是v11的攻击值
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%I64d",&mt[i].hp);
scanf("%I64d",&mt[i].atk);
mt[i].per=(mt[i].atk+0.0)/mt[i].hp; //怪兽单位量的血所具有的攻击能力
}
sort(mt+1,mt+n+1,cmp);
long long total=0;
int k=0;
for(int i=1;i<=n;i++)
{
k++;
while(mt[i].hp>0)
{
total+=mt[i].atk*k; //v11被消耗掉血
mt[i].hp-=m; //将怪兽的血减少
}
}
kcase++;
printf("Case #%d: %I64d\n",kcase,total);
}
return 0;
}
改正之后的AC代码
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef struct node
{
int hp;
int atk;
int t;
double per;
}Node;
Node mt[10003];
bool cmp(Node a,Node b)
{
return a.per>b.per;
}
int main()
{
int t,kcase=0;;
cin>>t;
while(t--)
{
int n,m;//m是v11的攻击值
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&mt[i].hp);
scanf("%d",&mt[i].atk);
mt[i].t=(mt[i].hp/m+((mt[i].hp%m)>0?1:0)); //打死它所需花费的时间
mt[i].per=(mt[i].atk+0.0)/mt[i].t; //怪兽单位时间所具有的攻击能力
}
sort(mt+1,mt+n+1,cmp);
//for(int i=1;i<=n;i++)
//cout<<" ( "<<mt[i].atk<<" , "<<mt[i].hp<<" , "<<mt[i].per<<" , "<<mt[i].t<<" ) "<<endl;
long long total=0;
int k=0;
for(int i=1;i<=n;i++)
{
k+=mt[i].t;
total+=mt[i].atk*k;
}
kcase++;
printf("Case #%d: %I64d\n",kcase,total);
}
return 0;
}
浙公网安备 33010602011771号