Submission #1351568

#TimeUsernameProblemLanguageResultExecution timeMemory
1351568top1svtinGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
0 / 100
0 ms344 KiB
#include <bits/stdc++.h>

#define kien long long
#define FOR(i, a, b) for (int i = a;i <= b; i++)
#define FORD(i, a, b) for (int i = a;i >= b; i--)
#define task "a"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)

using namespace std;
const int mxn = 2e5 + 5;
kien a[mxn], n, pre[mxn], suf[mxn], va[mxn], vb[mxn];
kien ans;

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen(task".inp", "r")) {
        fin(task); fou(task);
    }
    cin >> n;
    FOR (i, 1, n) cin >> a[i];

    // vb[1] = a[1]; x[1] = 0;
    pre[1] = 0;
    FOR (i, 2, n) {
        if (a[i - 1] < a[i]) pre[i] = pre[i - 1];
        else pre[i] = pre[i - 1] + (a[i - 1] - a[i] + 1);
        va[i] = a[i] + pre[i];
    }

    suf[n] = 0;
    FORD (i, n - 1, 1) {
        if (a[i + 1] < a[i]) suf[i] = suf[i + 1];
        else {
            suf[i] = suf[i + 1] + (a[i + 1] - a[i] + 1);
        }

        vb[i] = a[i] + suf[i];
    }

    ans = LLONG_MAX;
    FOR (i, 1, n) {
        if (va[i] > vb[i + 1])
            ans = min(ans, max(pre[i], suf[i + 1]));
        else
            ans = min(ans, max(pre[i] + (vb[i + 1] - va[i] + 1), suf[i + 1]));
    }

    cout << ans;
}

Compilation message (stderr)

Main.cpp:15:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   15 | main() {
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:7:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | #define fin(x) freopen(x".inp","r",stdin)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:19:9: note: in expansion of macro 'fin'
   19 |         fin(task); fou(task);
      |         ^~~
Main.cpp:8:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 | #define fou(x) freopen(x".out","w",stdout)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:19:20: note: in expansion of macro 'fou'
   19 |         fin(task); fou(task);
      |                    ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...