Similar to the queuing water issue;
Define the cost-effectiveness of each monster Bi = the attacking power of the monster/the number of hits on the monster in V11.
Perform an sort operation for Bi from large to small, and add each time.
It can be proved that the loss value will increase when the cost-performance ratio is small and the result will not be affected if the cost-performance ratio is the same.
Code:
Use Longlong.
#include <cstdio>#include <algorithm>using namespace std;const int maxn=10010;typedef long long typec;struct Monster{ typec hp,g,cnt; double bi;}m[maxn];bool cmp (Monster a,Monster b){ return a.bi>b.bi;}int main (){ int cas; scanf("%d",&cas); for (int I=1 ; I<=cas ; ++I) { int n,atk; scanf("%d%d",&n,&atk); for (int i=0 ; i<n ; ++i) { scanf("%lld%lld",&m[i].hp,&m[i].g); m[i].cnt=((m[i].hp+atk-1)/atk); m[i].bi=(double)m[i].g/(double)m[i].cnt; } sort (m,m+n,cmp); typec ans=0,cnt=0; for (int i=0 ; i<n ;++i) { cnt+=m[i].cnt; ans+=cnt*m[i].g; } printf("Case #%d: %lld\n",I,ans); } return 0;}