Result: Accepted
Time: 198ms
Memory: 2104kB
#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;
}