# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1279520 | ducanh0811 | Growing Vegetables is Fun 4 (JOI21_ho_t1) | C++20 | 1 ms | 572 KiB |
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define REV(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
using namespace std;
#define MAXN 200002
int a[MAXN];
int n;
int sum = 0;
void DQ(int l, int r){
int fl = l, fr = r;
while (a[fl] > a[fl - 1]) fl++;
while (a[fr] > a[fr + 1]) fr--;
if (fl > fr) {
if (a[fl] == a[fr]) sum++;
return;
}
int mi = min(a[fl - 1] + 1 - a[fl], a[fr + 1] + 1 - a[fr]);
a[fl] += mi;
a[fr] += mi;
sum += mi;
DQ(fl, fr);
}
void solve(){
cin >> n;
FOR(i, 1, n){
cin >> a[i];
}
DQ(1, n);
cout << sum;
}
#define task "test"
int32_t main(){
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |