Result: Accepted
Time: 78ms
Memory: 4844kB
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<math.h>
#include<vector>
#include<stack>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<stdio.h>
#include<functional>
#include<time.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const ll mode = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846264338327950;
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); }
template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); }
ll a[maxn];
ll cha[maxn],n;
ll jdz(ll kkk)
{
if(kkk<0)kkk=-kkk;
return kkk;
}
ll solve(ll minn)
{
ll sum=0;ll k;
for(ll i=1;i<=(n+1)/2;i++)
{
k=a[i]-(minn+i-1);
sum+=jdz(k);
}
for(ll i=(n+1)/2+1;i<=n;i++)
{
k=a[i]-(minn+(n+1)/2-(i-(n+1)/2));
if(n%2==1) k++;sum+=jdz(k);
}
//printf("%lld %lld\n",minn,sum);
return sum;
}
int main()
{
ll minn=maxn;
scanf("%lld",&n);
for(ll i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
if(a[i]<minn) minn=a[i];
}
for(ll i=1;i<=(n+1)/2;i++)
cha[i]=a[i]-(minn+i-1);
for(ll i=(n+1)/2+1;i<=n;i++)
{
cha[i]=a[i]-(minn+(n+1)/2-(i-(n+1)/2));
if(n%2==1) cha[i]++;
}
sort(cha+1,cha+1+n);ll js1=0,js2=n+1;
for(ll i=1;i<=n;i++)
if(cha[i]<0) js1=max(i,js1);
else if(cha[i]>0) js2=min(js2,i);
ll ans=0,flag=0;
if(js1==n+1-js2)
ans=0;
while(js1>n+1-js2)
{
ans--;
while(cha[js1]==ans)
js1--;
while(cha[js2]>=ans-1)
js2--;
js2++;
flag=1;
//printf("%d %d %d\n",ans,js1,js2);
}
if(flag==0)
{
while(js1<n+1-js2)
{
ans++;
while(cha[js1]<=ans-1)
js1++;
while(cha[js2]==ans)
js2++;
js1--;
//printf("%d %d %d\n",ans,js1,js2);
}
}
ans+=minn;
//printf("%lld\n",ans);
ll anss=min(solve(ans),solve(ans-1),solve(ans+1));
anss=min(anss,solve(ans-2),solve(ans+2));
printf("%lld\n",anss);
return 0;
}