제출 #1258846

#제출 시각아이디문제언어결과실행 시간메모리
1258846norman165Growing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
15 ms6620 KiB
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define yes() cout << "YES\n"
#define no() cout << "NO\n"

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 3e5;
const int mod1 = 998244353;
const int mod2 = 1e18 + 1;
const int mod3 = 1e9 + 9;
const int mod4 = 333333333;
const int mod5 = 200000;
const int mod6 = 10007;
const int k = 3000;
const int w = 1e5;
const ld EPS = 1e-8;

int LOG = 30;

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

    vector<int> a(n);
    for (int& i : a) cin >> i;

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

        if (b[i - 1] <= 0) pref1[i] += (-b[i - 1] + 1);
        if (b[i - 1] >= 0) pref2[i] += b[i - 1] + 1;
    }

    int ans = inf;
    for (int i = 0; i < n; i++) {
        int cur = pref1[i + 1];
        int res = pref1[i + 1];

        int x = pref2[n] - pref2[i + 1];
        if (x >= cur) res += (x - cur);
        ans = min(ans, res);
    }

    cout << ans << "\n";
}

signed main() {
#ifdef LOCAL
#else
    // freopen("qual" ".in", "r", stdin);
    // freopen("qual" ".out", "w", stdout);
#endif

    // cout.precision(16);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...