제출 #1109485

#제출 시각아이디문제언어결과실행 시간메모리
1109485DobromirAngelovGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
100 / 100
20 ms7948 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; const int MAXN=2e5+5; const long long INF=1e18+5; int n; int a[MAXN]; int l[MAXN], r[MAXN]; long long pref[MAXN], suff[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=2;i<=n;i++) l[i]=max(0, a[i-1]-a[i]+1); for(int i=n-1;i>=1;i--) r[i]=max(0, a[i+1]-a[i]+1); for(int i=2;i<=n;i++) pref[i]=pref[i-1]+l[i]; for(int i=n-1;i>=1;i--) suff[i]=suff[i+1]+r[i]; long long ans=INF; for(int i=1;i<=n;i++) { long long p=pref[i-1], s=suff[i+1]; long long cur=max(p,s); long long le=0, mid=0, ri=0; mid=a[i]+cur; if(i>1) le=a[i-1]+p; if(i<n) ri=a[i+1]+s; long long need=max(le,ri)+1-mid; if(need>0) cur+=need; ans=min(ans, cur); } cout<<ans<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...