Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
Output
Sample Input
10 2
2
100 1
1 100
Sample Output
201
分析:用贪心解决。血量少攻击高的要先消灭,所以以HP/DPS作为参数排序,即将所有的敌人
根据DPS/HP从大到小排序,如果相等,则按HP从小到大排序。记得要把测试样例删了,
忘了删测试样例WA了一次之后一血没了- -。
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct sa
{
int hp,dps;
double c;
}data[];
bool cmp(sa x,sa y)
{
return x.c>y.c;
}
int main()
{
int i,n,t,sum,ans;
while(cin>>n)
{
sum=;ans=;
memset(data,,sizeof(data));
for(i=;i<n;i++)
{
cin>>data[i].dps>>data[i].hp;
data[i].c=double(data[i].dps)/double(data[i].hp);
sum+=data[i].dps;
}
sort(data,data+n,cmp);
for(i=;i<n;i++)
{
ans+=data[i].hp*sum;
sum-=data[i].dps;
//cout<<ans<<endl;
}
cout<<ans<<endl;
}
return ;
}