普通的贪心题。排序后遍历看杀死每个怪需要多少血,加起来即可。刚开始得数对的,但是A不掉。搜了下题解,结果居然有人说是double,题上不明写着 each contains two integers DPSi and HPi吗,我真是日了狗了。。。最后发现是排序函数有问题,看来还是图样奶义务啊。。。
#include <stdio.h>
#include <string.h>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 5005;
const int INF = 1<<27;
struct HERO
{
int hp;
int dps;
}hero[N];
/*bool cmp(HERO a, HERO b)
{
if(a.dps != b.dps)
return a.dps > b.dps;
else
return a.hp < b.hp;
}*/
bool cmp(const HERO &a,const HERO &b)
{
return a.hp*b.dps<a.dps*b.hp;
}
int main()
{
// freopen("in.txt", "r", stdin);
int n, i, ans, sum;
while(~scanf("%d", &n) )
{
sum = 0;
for(int i = 1; i <= n; i ++)
{
scanf("%d%d", &hero[i].dps, &hero[i].hp);
sum += hero[i].dps;
}
sort(hero + 1, hero + n + 1, cmp);
ans = 0;
for(i = 1; i <= n; i ++)
{
ans += sum * hero[i].hp;
sum -= hero[i].dps;
}
printf("%d\n", ans);
}
return 0;
}
本文介绍了一个使用贪心算法解决的游戏打怪问题。通过对怪物按特定条件排序,可以有效地计算出击败所有怪物所需的总血量。文章提供了一份C++实现代码,并解决了排序函数中存在的问题。
&spm=1001.2101.3001.5002&articleId=50643048&d=1&t=3&u=64a66a791d1d47d5b8fa437a35341719)
489

被折叠的 条评论
为什么被折叠?



