제출 #469308

#제출 시각아이디문제언어결과실행 시간메모리
469308vincent97198Growing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
1 ms204 KiB
#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;

const double eps=1e-6;

int32_t main()
{
    int n;  cin >> n;
    vector<int> h(n+1),L(n+2,0),R(n+2,0);
    for(int i=1;i<=n;++i)    cin >> h[i];

    for(int i=n;i>=1;--i){
        R[i]=R[i+1];
        if(i+1<=n && h[i+1]>=h[i])
            R[i]+=h[i+1]-h[i]+1;
    }

    for(int i=1;i<=n;++i){
        L[i]=L[i-1];
        if(i-1>=1 && h[i-1]>=h[i])
            L[i]+=h[i-1]-h[i]+1;
    }

    int ans=1e18;
    for(int i=1;i<=n;++i){
        int tmpL=0,tmpR=0;
        if(i-1>=1 && h[i]+R[i+1]<=h[i-1])
            tmpL=h[i-1]-(h[i]+R[i+1])+1;
        if(i+1<=n && h[i]+L[i-1]<=h[i+1])
            tmpR=h[i+1]-(h[i]+L[i-1])+1;
        ans=min(ans,R[i+1]+L[i-1]+tmpL+tmpR);
    }
    cout << ans << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...