题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6709
思路:先抓煮的时间多的鱼,把煮鱼时间放进优先队列里,每次找剩余煮鱼时间最多的时候的去抓鱼,然后时间减去抓鱼时间,再放到队列里。最后再把队列里的数加起来
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<queue>
#define ll long long
using namespace std;
ll A[200010];
priority_queue <ll,vector<ll>,less<ll>> q;
int main()
{
ll t,n,m;
for(scanf("%lld",&t); t; t--)
{
scanf("%lld%lld",&n,&m);
for(int i=1; i<=n; i++)
scanf("%lld",&A[i]);
ll ans=m,x;
sort(A+1,A+n+1,greater<ll>());
for(int i=1; i<=n; i++)
{
if(i==n)
{
ans+=A[i];
break;
}
q.push(A[i]);
x=q.top();
q.pop();
ans+=m;
q.push(x-m<0?0:x-m);
}
while(!q.empty())
{
ans+=q.top();
q.pop();
}
printf("%lld\n",ans);
}
}