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: 198ms
Memory: 2104kB
Author: 2017212212283
In contest: 1261

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cmath>

#define MAXN 100005
using namespace std;
typedef long long ll;

int a[MAXN];
int n;

ll f(ll x)
{
	int index=(n-1)>>1;
	ll ans=0;
	for(int i=0;i<n;i++)
	{
		if(n&1)
		{
			ll lg=x-abs(index-i);
			ans+=abs(lg-a[i]);
		}else
		{
			if(i<=index)
			{
				ll lg=x-abs(index-i);
				ans+=abs(lg-a[i]);
			}
			else
			{
				ll lg=x-abs(index+1-i);
				ans+=abs(lg-a[i]);
			}
		}
	}
	return ans;
}

int main()
{
	int i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
		
	ll l=0,r=10000000000000;
	while(l<r-1)
	{
		ll mid=(l+r)>>1;
		ll mmid=(mid+r)>>1;
//		cout<<mid<<' '<<mmid<<' '<<f(mid)<<' '<<f(mmid)<<endl;
		if(f(mid)<f(mmid)) r=mmid;
		else l=mid;
//		cout<<l<<' '<<r<<endl;
	}
//	cout<<l<<' '<<r<<endl;
//	printf("%lld %lld\n",f(l),f(r));
	printf("%lld",min(f(l),f(r)));
	return 0;
}