Submission #378310

#TimeUsernameProblemLanguageResultExecution timeMemory
378310smjleoGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
100 / 100
28 ms5740 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
#define int long long
#define nl '\n'
#define io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1000000007, mod2 = 998244353;

// change this
const int N = 200005;

int n, arr[N], pf[N], sf[N], ans = 1e18;

signed main() {
    io;
    cin >> n;
    for (int i=0; i<n; i++) cin >> arr[i];
    for (int i=1; i<n; i++) {
        // calculate prefix
        pf[i] = pf[i-1];
        if (arr[i-1] < arr[i]) continue;
        pf[i] += arr[i-1] - arr[i] + 1;
    }   // pf[i]: number of operations to make 1...i strictly increasing
    for (int i=n-2; i>=0; i--) {
        // calculate suffix
        sf[i] = sf[i+1];
        if (arr[i] > arr[i+1]) continue;
        sf[i] += arr[i+1] - arr[i] + 1;
    }   // sf[i]: number of operations to make i...n strictly decreasing

    for (int i=0; i<n; i++) {
        ans = min(ans, max(pf[i], sf[i]));
    }

    cout << ans << nl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...