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: 136ms
Memory: 4100kB
Author: 2017212212172
In contest: 1261

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<functional>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;
int ax[100100];
int bn[100100];
priority_queue<LL>q1;
priority_queue<LL, vector<int>,greater<int>>q2;
int main(void) {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &ax[i]);
	}
	int mid;
	if (n % 2 == 1) {
		mid = n / 2 + 1;
	}
	else
		mid = n / 2;
	LL  sum;
	LL  tot = 0;
	for (int i = 1; i <= mid; i++) {
		bn[i] = i - ax[i];
		tot += abs(bn[i]);
	}

	for (int i = mid+1; i <= n; i++) {
		bn[i] = n-i+1 - ax[i];
		tot += abs(bn[i]);
	}
	int sum1 = 0;
	int sum2 = 0;
	int sum3 = 0;
	for (int i = 1; i <= n; i++) {
		if (bn[i] < 0) { 
			sum1++; 
			q1.push(bn[i]);
		}
		else if (bn[i] > 0) { 
			sum2++;
			q2.push(bn[i]);
		}
		else sum3++;
	}
	if (sum1 > sum2 + sum3) {
		LL  tt = 0;
		while (!q1.empty() && sum1 > sum2 + sum3) {
			LL  nd = q1.top();
			q1.pop();
			if (nd - tt == 0) {
				sum1--;
				sum3++;
				continue;
			}
			LL  ggg = nd;
			nd = nd - tt;
			tt = ggg;
			tot -= sum1*abs(nd);
			tot += (sum2 + sum3)*abs(nd);
			sum1--;
			sum3++;
		}
	}
	else if (sum2 > sum1 + sum3) {
		LL  tt = 0;
		while (!q2.empty() && sum2 > sum1 + sum3) {
			LL  nd = q2.top();
			q2.pop();
			if (nd - tt == 0) {
				sum2--;
				sum3++;
				continue;
			}
			LL ggg = nd;
			nd = nd - tt;
			tt =ggg;
			tot -= sum2*abs(nd);
			tot += (sum1 + sum3)*nd;
			sum2--;
			sum3++;
		}
	}
	printf("%lld\n", tot);
	return 0;
}