Submission #475814

#TimeUsernameProblemLanguageResultExecution timeMemory
475814Jarif_RahmanGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
100 / 100
34 ms14900 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    vector<ll> v(n);
    for(ll &x: v) cin >> x;
    auto vv = v;
    vector<pair<ll, ll>> prf(n), sff(n);
    vector<ll> prf1(n), sff1(n);
    prf[0] = {0, 0};
    prf1[0] = v[0];
    for(int i = 1; i < n; i++){
        ll ls = prf[i-1].sc;
        prf[i].f = prf[i-1].f, prf[i].sc = 0;
        ll x = v[i-1] - v[i] + 1;
        x = max(x, 0LL);
        prf[i].f+=x;
        prf[i].sc = ls+x;
        prf1[i] = max(v[i], v[i]+ls+x);
    }
    v = vv;
    sff[n-1] = {0, 0};
    sff1[n-1] = v[n-1];
    for(int i = n-2; i >= 0; i--){
        ll ls = sff[i+1].sc;
        sff[i].f = sff[i+1].f, sff[i].sc = 0;
        ll x = v[i+1] - v[i] + 1;
        x = max(x, 0LL);
        sff[i].f+=x;
        sff[i].sc = ls+x;
        sff1[i] = max(v[i], v[i]+ls+x);
    }
    v = vv;
    ll ans = 1e18;
    for(int i = 0; i < n; i++){
        ll cur = 0;
        ll x1 = 0, y1 = 0, x2 = 0, y2 = 0;
        tie(x1, x2) = prf[i];
        if(i!=n-1){
            tie(y1, y2) = sff[i+1];
        }
        cur+=x1+y1-min(x2, y2);
        if(i!=n-1 && prf1[i] == sff1[i+1]) cur++;
        ans = min(ans, cur);
    }
    cout << ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...