제출 #600377

#제출 시각아이디문제언어결과실행 시간메모리
6003771neGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
0 ms212 KiB
#include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n;cin>>n; vector<long long>arr(n); for (int i = 0;i<n;++i){ cin>>arr[i]; } vector<long long>pref(n),suff(n); vector<long long>prefmax(n),suffmax(n); vector<long long>maxxy(n,0),maxxy2(n,0); prefmax[0] = arr[0]; for (int i = 1;i<n;++i){ prefmax[i] = max(prefmax[i - 1] + 1,arr[i]); } suffmax[n - 1] = arr[n - 1]; for (int i = n - 2;i>=0;--i){ suffmax[i] = max(suffmax[i + 1] + 1,arr[i]); } pref[0] = 0; long long h = 0; for (int i =1;i<n;++i){ long long needed = max(0LL,prefmax[i - 1] + 1 - arr[i]); pref[i] = pref[i - 1] + max(0LL,prefmax[i - 1] + 1 - arr[i] - h); maxxy[i] = max(maxxy[i - 1],max(0LL,prefmax[i - 1] + 1 - arr[i] - h)); h = needed; } h = 0; suff[n - 1] = 0; for (int i =n - 2;i>=0;--i){ long long needed = max(0LL,suffmax[i + 1] + 1 - arr[i]); suff[i] = suff[i + 1] + max(0LL,suffmax[i + 1] + 1 - arr[i] - h); maxxy2[i] = max(maxxy2[i + 1],max(0LL,suffmax[i + 1] + 1 - arr[i] - h)); h = needed; } long long ans = 1e15; for (int i = 1;i<n - 1;++i){ ans = min(ans,pref[i - 1] + suff[i + 1] + max(0LL,suffmax[i] + 1 - prefmax[i - 1]) - min(maxxy[i - 1],maxxy2[i + 1])); } ans = min(ans,pref[n - 1]); ans = min(ans,suff[0]); cout<<ans<<'\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...