标签:ceil math mes names stream 10000+ return cas 最小
这题刚开始想当然的直接按g值排序了。
正确做法是,由于要失去的血量最小,则若此时有两个monster A,B
先A后B失去的血量为 (timeA+timeB)gB+timeAgA
先B后A失去的血量为(timeA+timeB)gA+timeBgB
按这个排序即可
!注意这里有个精度问题,long long才能过
#include <iostream>
#include <queue>
#include <algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define MAX 10000+10
int n, m;
struct M {
int hp, g;
double t;
}monster[MAX];
int cmp(const M&a, const M&b) {
return a.t*b.g < b.t*a.g;
}
int main(void) {
int t;
int Kase = 0;
scanf("%d", &t);
int Time;
while (t--) {
long long ans = 0;
Time = 0;
scanf("%d%d", &n, &m);
int hp, g;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &hp, &g);
monster[i].hp = hp;
monster[i].g = g;
monster[i].t = (__int64)ceil((double)monster[i].hp / (double)m);
}
sort(monster + 1, monster + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
Time += monster[i].t;
ans += (long long)monster[i].g*Time;
}
printf("Case #%d: %I64d\n", ++Kase, ans);
}
return 0;
}
标签:ceil math mes names stream 10000+ return cas 最小
原文地址:https://www.cnblogs.com/tennant/p/8901838.html