This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int n;
cin>>n;
int arr[n],a[n];
for(int i=0;i<n;i++){
cin>>arr[i];
a[i]=arr[i];
}
int pre[n+1]={},suf[n+1]={},ansP[n+1]={},ansS[n+1]={};
for(int i=1;i<n;i++){
if(pre[i]<=arr[i-1]-arr[i]){
pre[i]=((arr[i-1]-arr[i])-pre[i])+1;
arr[i]=arr[i-1]+1;
}
else if(arr[i-1]-arr[i]>0) pre[i]=(arr[i-1]-arr[i])+1;
}
for(int i=n-2;i>=0;i--){
if(suf[i]<=a[i+1]-a[i]){
suf[i]=((a[i+1]-a[i])-suf[i])+1;
a[i]=a[i+1]+1;
}
else if(a[i+1]-a[i]>0) suf[i]=(a[i+1]-a[i])+1;
}
int cnt=0;
for(int i=1;i<n;i++){
if(pre[i]-pre[i-1]>0) cnt+=pre[i]-pre[i-1];
ansP[i]=cnt;
}
cnt=0;
for(int i=n-2;i>=0;i--){
if(suf[i]-suf[i+1]>0) cnt+=suf[i]-suf[i+1];
ansS[i]=cnt;
}
int mn=1e18;
for(int i=0;i<n;i++){
if(i==0) mn=min(mn,ansS[i]);
else if(i==n-1) mn=min(mn,ansP[i]);
else {
int ANS=max(ansP[i],ansS[i+1]);
if(a[i+1]==arr[i]) ANS++;
mn=min(ANS,mn);
}
}
cout<<mn<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |