考虑DP式子之后,可以通过堆维护函数,求出对应值
code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 200005
int zu,n,d,tg,num;
int a[N];
priority_queue<int> q;
signed main(){
scanf("%lld",&zu);
while(zu--){
scanf("%lld%lld",&n,&d);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
sort(a+1,a+1+n);tg=num=0;
while(!q.empty()) q.pop();
for(int i=1;i<=n;i++) q.push(-d);
for(int i=1;i<=n;i++){
tg-=d;
if(q.empty()||a[i]+tg>=q.top()) q.push(a[i]+tg);
else{
q.push(a[i]+tg),q.push(a[i]+tg);
int x=q.top();q.pop();
num=num+x-(a[i]+tg);
}
}
printf("%lld\n",num);
}
return 0;
}
标签:25,2024.2,int,题解,num,zu,push,tg,lld
From: https://www.cnblogs.com/hubingshan/p/18035608