This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
int rnd(int x, int y) { // random number generator
int u= uniform_int_distribution<int>(x, y)(rng); return u;
}
void solve(int tc) {
int N;
cin >> N;
int a[N+1];
for(int i=1; i<=N; i++) cin >> a[i];
if(N == 2) {
cout << (a[1] == a[2] ? 1 : 0) << "\n";
return;
}
int l = 2, r = N - 1, d = 0, ans = 0;
int now[N+1] = {};
while(l <= r) {
if(!now[l]) a[l] += d;
if(!now[r] && l != r) a[r] += d;
now[l] = now[r] = 1;
int x = max(0ll, a[l-1] + 1 - a[l]);
int y = max(0ll, a[r+1] + 1 - a[r]);
d += min(x, y);
ans += min(x, y);
a[l] += min(x, y);
if(l != r) a[r] += min(x, y);
if(x < y) l++;
else r--;
}
if(a[l] == a[r] && l != r) ans++;
cout << ans << "\n";
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |