# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
393078 | ahmet | Growing Vegetables is Fun 4 (JOI21_ho_t1) | C++14 | 1 ms | 460 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define zaman cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds "
#define rep(i,n) for(long long (i)=0;(i)<(n);++(i))
#define ref(i,a,b) for (long long (i)=(a); (i)<=(b); ++(i))
#define endl '\n'
#define st first
#define nd second
#define ll long long
#define pb push_back
#define pii pair<ll,ll>
#define mp make_pair
const int mx=2e5+6;
ll a[mx];
ll l[mx],r[mx],b[mx],c[mx],ka[mx],kb[mx];
ll ans=1e18;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin >> n;
ref(i,1,n){
cin >> a[i];
}
ll val=a[1]+1;
ll res=0,need;
ka[1]=a[1];
for(int i=2;i<=n;++i){
if(a[i]>val){
ka[i]=val;
val=a[i]+1;
l[i]=l[i-1];
b[i]=0;
continue;
}
need=val-a[i];
if(need<=b[i-1]){
l[i]=l[i-1];
}
else{
l[i]=l[i-1]+need-b[i-1];
}
b[i]=need;
ka[i]=val;
++val;
}
val=a[n]+1;
res=0;
kb[n]=a[n];
for(int i=n-1;i>=1;--i){
if(a[i]>val){
kb[i]=val;
val=a[i]+1;
r[i]=r[i+1];
c[i]=0;
continue;
}
need=val-a[i];
if(need<=c[i+1]){
r[i]=r[i+1];
}
else{
r[i]=r[i+1]+need-c[i+1];
}
c[i]=need;
kb[i]=val;
++val;
}
ans=min(r[1],l[n]);
for(int i=2;i<=n-1;++i){
ll left=ka[i-1];
ll right=kb[i+1];
ll lel=max(c[i+1],b[i-1]);
ll kel=max(left,right)+1;
need=kel-a[i];
if(need<0)need=0;
ll efl=min(c[i+1],b[i-1]);
//cout << i << " " <<need<< " "<< l[i-1]+r[i+1]+need-efl << endl;
ans=min(ans,l[i-1]+r[i+1]+need-efl);
}
cout << ans << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |