Submission #1215248

#TimeUsernameProblemLanguageResultExecution timeMemory
1215248sunflowerGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
100 / 100
14 ms4936 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define SZ(x) ((int) (x).size())
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define debug(x) cout << "[" << #x << " = " << (x) << "]" << endl

#define left    __left
#define right   __right
#define prev    __prev
#define next    __next
#define fi      first
#define se      second

template <class X, class Y>
    bool maximize(X &x, Y y) {
        if (x < y) return x = y, true;
        else return false;
    }

template <class X, class Y>
    bool minimize(X &x, Y y) {
        if (x > y) return x = y, true;
        else return false;
    }

#define MAX_N 200'200
int a[MAX_N + 2], b[MAX_N + 2];
ll prefix[MAX_N + 2], suffix[MAX_N + 2];
int n;

int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    #define task "test"
    if (fopen(task".inp","r")) {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }

    cin >> n;
    FOR(i, 1, n) cin >> a[i];

    FOR(i, 1, n - 1) b[i] = a[i + 1] - a[i];

//    peak = K;
/**
    1.. K - 1 (b[i] >= 1)
    K ... n - 1 (b[i] <= -1);
**/

///    update[L..R]: b[i];
///    b[L] += 1;
///    b[R] -= 1;


//    FOR(i, 0, cout << x << " ";

    prefix[0] = 0;
    FOR(i, 1, n - 1) {
        prefix[i] = prefix[i - 1];
        if (b[i] < 1) prefix[i] += 1 - b[i];
    }

    suffix[n] = 0;
    FORD(i, n - 1, 1) {
        suffix[i] = suffix[i + 1];
        if (b[i] > -1) suffix[i] += b[i] - (-1);
    }

    ll ans = 1e18;
    FOR(peak, 1, n) {
//        ll prefix = 0;
//        FOR(i, 1, peak - 1) if (b[i] < 1) prefix += 1 - b[i];
//
//        ll suffix = 0;
//        FOR(i, peak, n - 1) if (b[i] > -1) suffix += b[i] - (-1);

        minimize(ans, max(prefix[peak - 1], suffix[peak]));
    }

    cout << ans;

    return 0;
}

/* Discipline - Calm */

Compilation message (stderr)

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