Problem - 4310 (dingbacode.com)https://acm.dingbacode.com/showproblem.php?pid=4310当然是先选择攻击值最高的打了,一开始这么想,但是错了,因为攻击值高的可能血量也很高,很难打败。
所以,应该按照Dps/Hp从高到低的顺序开始攻击,Dps/Hp值越大,说明能最快速的削弱其战斗力,也说明排在前面的是攻击值相对较大,血量相对较少的。
然后在输入数据的时候,记录所有攻击值的总和,每打败一个,那么就在总和中减去它的攻击值。
#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct hero {
int dps;
int hp;
double hpAvg;
bool operator<(const hero &a) const {
return hpAvg > a.hpAvg;
}
} hero;
int main() {
int n;
int totalDps;
int totalHp;
int loss;
hero heros[22];
while (scanf("%d", &n) != EOF) {
totalDps = 0;
totalHp = 0;
for (int i = 0; i < n; ++i) {
scanf("%d %d", &heros[i].dps, &heros[i].hp);
heros[i].hpAvg = 1.0 * heros[i].dps / heros[i].hp;
totalDps += heros[i].dps;
totalHp += heros[i].hp;
}
sort(heros, heros + n);
loss = 0;
//击败n个英雄
for (int i = 0; i < n; ++i) {
int tempHp = heros[i].hp;
while (tempHp > 0) {
tempHp--;
loss += totalDps;
}
totalDps -= heros[i].dps;
}
printf("%d\n", loss);
}
return 0;
}