이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define sz(v) v.size()
#define up_b upper_bound
#define low_b lower_bound
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
const int N=2e5+11;
const ll mod=1e9+7;
const ll INF=1e18;
ll a[N],b[N];
pair<ll,ll>dp1[N],dp2[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=a[i];
}
ll ans=0;
ll cur=0;
for(int i=1;i<=n;i++){
if(b[i]>b[i-1]){
cur=0;
dp1[i]=mp(ans,cur);
}
else{
if(b[i]+cur<=b[i-1]){
ans+=b[i-1]-cur-b[i]+1;
cur+=b[i-1]-cur-b[i]+1;
b[i]=b[i-1]+1;
}
else{
cur-=b[i]+cur-b[i-1]-1;
b[i]=b[i-1]+1;
}
dp1[i]=mp(ans,cur);
}
}
ans=0;
cur=0;
for(int i=1;i<=n;i++)b[i]=a[i];
for(int i=n;i>=1;i--){
if(b[i]>b[i+1]){
cur=0;
dp2[i]=mp(ans,cur);
}
else{
if(b[i]+cur<=b[i+1]){
ans+=b[i+1]-cur-b[i]+1;
cur+=b[i+1]-cur-b[i]+1;
b[i]=b[i+1]+1;
}
else{
cur-=b[i]+cur-b[i+1]-1;
b[i]=b[i+1]+1;
}
dp2[i]=mp(ans,cur);
}
}
ll res=INF;
for(int i=1;i<=n;i++){
ll x=a[i]+min(dp1[i-1].y,dp2[i+1].y);
// cout<<dp1[i-1].x<<" "<<dp2[i+1].x<<" "<<dp1[i-1].y<<" "<<dp2[i+1].y<<endl;
res=min(res,dp1[i-1].x+dp2[i+1].x-min(dp1[i-1].y,dp2[i+1].y)+max(0ll,max(a[i+1],a[i-1])+1-x));
}
cout<<res;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |