Submission #646882

#TimeUsernameProblemLanguageResultExecution timeMemory
646882perchutsGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
100 / 100
31 ms10088 KiB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define pb push_back
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ii = pair<int,int>;
using iii = tuple<int,int,int>;

const int inf = 2e9+1;
const int mod = 1e9+7;
const int maxn = 3e5+100;

template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }

pair<ll,ll> suf[maxn], pref[maxn];

int main(){_
    int n;cin>>n;

    vector<ll>v(n);

    for(auto&x:v)cin>>x;

    ll cur = 0, usando = 0;

    for(int i=1;i<=n;++i){
        ll need = max(0ll, cur - v[i-1] + 1);
        pref[i].first = pref[i-1].first + max(0ll, need - usando);
        usando = need;
        pref[i].second = usando;
        ckmax(cur, max(v[i-1], cur+1));
    }

    cur = 0, usando = 0;

    for(int i=n;i>=1;--i){
        ll need = max(0ll, cur - v[i-1] + 1);
        suf[i].first = suf[i+1].first + max(0ll, need - usando);
        usando = need;
        suf[i].second = usando;
        ckmax(cur, max(v[i-1], cur+1));
    }


    ll resp = min(suf[1].first, pref[n].first);

    for(int i=1;i<=n;++i){
        ckmin(resp, max(pref[i].first, suf[i].first));
    }

    cout<<resp<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...