This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);
template<typename T> void _do(T x){cerr<<x<<"\n";}
template<typename T,typename ...U> void _do(T x,U ...y){cerr<<x<<", ";_do(y...);}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
int N;
cin >> N;
vector<ll> arr(N);
for (auto &i: arr) cin >> i;
int lidx = 1, ridx = N - 2;
while (lidx < N && arr[lidx] > arr[lidx - 1]) lidx++;
while (ridx >= 0 && arr[ridx] > arr[ridx + 1]) ridx--;
if (lidx == N || ridx == -1) {
cout << 0 << '\n';
return 0;
}
ll answer = 0;
while (lidx <= ridx) {
ll rdif = arr[ridx + 1] - arr[ridx];
ll ldif = arr[lidx - 1] - arr[lidx];
ll add = min(rdif, ldif) + 1;
answer += add;
for (int i = lidx; i <= ridx; i++) arr[i] += add;
// for (int i = 0; i < N; i++) cout << arr[i] << " \n"[i == N - 1];
// arr[ridx + 1] -= add;
// arr[lidx - 1] -= add;
while (lidx < N && arr[lidx] > arr[lidx - 1]) lidx++;
while (ridx >= 0 && arr[ridx] > arr[ridx + 1]) ridx--;
if (lidx > ridx) break;
}
// cout << answer << " " << lidx << " " << ridx << '\n';
if (arr[lidx] == arr[ridx]) answer++;
cout << answer << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |