Submission #1357512

#TimeUsernameProblemLanguageResultExecution timeMemory
1357512pqhxappleGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
18 ms5108 KiB
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)

using ll = long long;
const int MAXN = 2e5 + 10;

int n;
ll a[MAXN];
ll pref[MAXN], suff[MAXN];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    FOR(i, 1, n) cin >> a[i];

    // pref[i]: min ops to make a[1..i] strictly increasing
    ll cur = a[1];
    ll prev_add = 0; // cur - a[i] at previous position
    pref[1] = 0;

    FOR(i, 2, n) {
        cur = max(cur + 1, a[i]);
        ll add = cur - a[i];
        pref[i] = pref[i - 1] + max(0LL, add - prev_add);
        prev_add = add;
    }

    // suff[i]: min ops to make a[i..n] strictly decreasing
    cur = a[n];
    prev_add = 0;
    suff[n] = 0;

    FORD(i, n - 1, 1) {
        cur = max(cur + 1, a[i]);
        ll add = cur - a[i];
        suff[i] = suff[i + 1] + max(0LL, add - prev_add);
        prev_add = add;
    }

    ll res = (ll)4e18;
    FOR(i, 1, n) {
        res = min(res, max(pref[i], suff[i]));
    }

    cout << res << '\n';
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...