Start: Feb, 21, 2019 12:00:00
2018-2019 ACM集训队冬季集训第二次考核
End: Feb, 21, 2019 17:00:00
Time elapsed:
Time remaining:

Problem_ID: B
Result: Accepted
Time: 169ms
Memory: 5624kB
Author: 2018212212307
In contest: 1261

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e6+5;
int n;
int x[maxn];
ll f(int y)
{
	ll p=0;
	for(int i=1;i<=(n)/2;i++)
	{
		p+=abs(x[i]-i+1-y);
	}
//	if(n&1)
//	{
//		p+=abs(x[n/2+1]-y-(int)n/2-1);
//	}
	for(int i=n;i>(int)n/2;i--)
	{
		int h=n-i+1;
		p+=abs(x[i]-h+1-y);
	}
	return p;
}
int main()
{
	cin>>n;
	ll ans=10000000000000000;
	ll p=0;
	int flag=1;
	for(int i=1;i<=n;i++)scanf("%d",&x[i]);
	for(int i=2;i<=n/2;i++)
	{
		if(x[i]!=x[i-1]+1)
		{
			flag=0;
			break;
		}
	}
	for(int i=n;i>=n/2;i--)
	{
		if(x[i]!=x[n-i+1])
		{
			flag=0;
			break;
		}
	}
	if(flag)
	{
		printf("0\n");;
		return 0;
	}
	int l=-1000000,r=1000000;
	while(l<r)
	{
		int mid=(l+r)/2;
		ll p=f(mid);
		ll p1=f(mid-1);
		ll p2=f(mid+1);
		for(int i=mid-5;i<=mid+5;i++)
		{
			ans=min(ans,f(i));
		}
		if(p>p1&&p2>p)
		{
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}

		
	}
	printf("%lld",ans);

}