#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;
void solve(){
ll n, i, l, r, res=0, k;
cin>>n;
vector<ll> a(n);
vector<ll> b(n);
vector<ll> c(n);
for(i=0;i<n;++i){
cin>>a[i];
}
for(i=1;i<n;++i){
b[i] = a[i] - a[i-1] - 1;
}
for(i=n-2;i>=0;--i){
c[i] = a[i] - a[i+1] - 1;
}
l = 1;
r = n-1;
while(l < n and b[l] >= 0){
++l;
}
while(r >= 0 and c[r] >= 0){
--r;
}
while(l <= r){
if(l == r){
res += max({0ll, -b[l], -c[r]});
break;
}else if(l+1 == r){
res += max({0ll, -b[l], -c[r]});
++l;
--r;
break;
}
k = min(-b[l], -c[r]);
res += k;
b[l] += k;
c[r] += k;
while(l < n and b[l] >= 0){
++l;
}
while(r >= 0 and c[r] >= 0){
--r;
}
}
if(l < n and r >= 0 and a[l] == a[r]) ++res;
cout<<res<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(nullptr));
ll t=1;
// cin>>t;
for(;t>0;--t){
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |