Fight Against Monsteres_贪心
程序员文章站
2022-06-06 17:50:04
...
本题按照性价比排序依次打怪兽;
每只怪兽的攻击力/打每只怪兽的次数 排序
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
typedef long long ll;
int n,m;
struct node
{
ll hp,at;
double bi;
}spe[maxn];
ll judge(ll a)
{
for(ll i = 1; i <= a; i++ )
if((i*(i+1)/2) >=a)
return i;
}
int cmp(node a,node b)
{
return a.bi>b.bi;
}
int main()
{
int ant = 1;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
for(int i = 0; i < m; i++)
{
scanf("%lld%lld",&spe[i].hp,&spe[i].at);
spe[i].hp = judge(spe[i].hp);
spe[i].bi =((double)spe[i].at/(double)spe[i].hp);
}
sort(spe,spe+m,cmp);
ll sum = 0,cnt = 0;
for(int i = 0; i < m; i++)
{
sum += (spe[i].hp+cnt)*spe[i].at;
cnt += spe[i].hp;
}
printf("Case #%d: %lld\n",ant++,sum);
}
return 0;
}