Submission #1282175

#TimeUsernameProblemLanguageResultExecution timeMemory
1282175swishy123Growing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
17 ms5124 KiB
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int def = 1e6+1;
const int maxk = 18;
const ll inf = 1e18;

void solve(){
    int n;
    cin >> n;

    vector<int> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];

    a.push_back(0);
    n++;

    vector<int> b(n, 0);
    for (int i = 1; i < n; i++)
        b[i] = a[i] - a[i - 1];

    b[0] = a[0];
    vector<ll> pref(n, 0ll);
    vector<ll> suff(n, 0ll);

    ll crr = 0;
    for (int i = 0; i < n; i++){
        crr += max(-b[i] + 1, 0);
        pref[i] = crr;
    }
    crr = 0;
    for (int i = n - 1; i >= 0; i--){
        crr += max(b[i] + 1, 0);
        suff[i] = crr;
    }

    ll res = inf;
    for (int i = 0; i < n - 1; i++)
        res = min(res, max(pref[i], suff[i + 1]));
    cout << res;
}

/*
0 0 0 0 0
*/

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

    if (ifstream("input.txt").good()){
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout); 
    }

    int t = 1;
    while (t--){
        solve();
        cout << "\n";
    }
}

Compilation message (stderr)

Main.cpp:50:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   50 | main(){
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:55:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:56:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...