Submission #1355789

#TimeUsernameProblemLanguageResultExecution timeMemory
1355789truongthaiduonglaptrinhGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
0 / 100
0 ms356 KiB
// duonglaptrinh

# include <bits/stdc++.h>
using namespace std;

const int N = 2e5+5;

int n;
long long a[N], l[N], r[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    if(fopen("nhap.inp", "r")) {
        freopen("nhap.inp", "r", stdin);
        freopen("nhap.out", "w", stdout);
    }

    cin>>n;

    for(int i = 1; i <= n; i++) {
        cin>>a[i];
    }

    a[0] = a[1] - 1, a[n + 1] = a[n] + 1;

    int i = 1, last = 0, s = 0;
    while(i <= n) {
        if(last) {
            s += abs(a[i] - a[last]) + 1;
        }
        l[i] = s;
        int j = i + 1;
        while(j <= n && a[j - 1] < a[j]) {
            l[j] = s;
            j++;
        }
        last = j - 1;
        i = j;
    }

    i = n, last = 0, s = 0;
    while(i >= 1) {
        if(last) {
            s += abs(a[i] - a[last]) + 1;
        }
        r[i] = s;
        int j = i - 1;
        while(j >= 1 && a[j] > a[j + 1]) {
            r[j] = s;
            j--;
        }
        last = j + 1;
        i = j;
    }

    long long ans = l[n];
    for(int i = 1; i <= n; i++) {
        long long s = max(l[i - 1], r[i + 1]);
        s += max(0LL, max(a[i - 1] - a[i] + 1, a[i + 1] - a[i] + 1));
        ans = min(ans, s);
    }

    cout<<ans;

    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         freopen("nhap.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         freopen("nhap.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...