题解:P11301 [NOISG 2021 Finals] Password
下文认为是把
首先我们知道每个位置需要操作的次数形如
假设你知道了最后每个位置被操作的真实次数
你发现你给一个位置
然后来分析一下,首先一个位置要么将
然后给初始
到这里是一个二分图最大权匹配模型(
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 3e5+114;
int n,a[maxn],b[maxn],k;
int ans;
priority_queue<int> P;
int cZ;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>k;
k++;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
if(b[i]>=a[i]) a[i]=b[i]-a[i];
else a[i]=b[i]+k-a[i];
}
for(int i=n;i>=1;i--) a[i]-=a[i-1],ans+=max(0*1ll,a[i]);
for(int i=n;i>=1;i--){
if(a[i]>0){
cZ+=(a[i]/k);
P.push(a[i]%k);
}else if(a[i]<0){
int cnt=(-a[i])/k;
int f=(-a[i])%k-k;
int p=min(cZ,cnt);
ans-=p*k;
cZ-=p,cnt-=p;
while(cnt>0&&P.size()>0){
ans-=P.top();
P.pop();
cnt--;
}
if(f==-k) continue;
if(cZ>0){
cZ--;
ans-=(k+f);
P.push(-f);
}else{
if(P.size()>0&&P.top()+f>0){
ans-=(P.top()+f);
P.pop();
P.push(-f);
}
}
}
}
cout<<ans<<'\n';
return 0;
}