记录编号 446870 评测结果 AAAAAAAAAA
题目名称 [Codeforces 819B] B先生和PR移位 最终得分 100
用户昵称 GravatarAAAAAAAAAA 是否通过 通过
代码语言 C++ 运行时间 0.362 s
提交时间 2017-09-09 08:02:35 内存使用 15.55 MiB
显示代码纯文本
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cmath>
#define LL long long
#define MAXN 2000010
#define INF 0x7fffffff
using namespace std;
LL ans,now;
int N, p[MAXN], l, r, k, sum[MAXN];
int main() {
	freopen("MrBB1.in", "r", stdin);
	freopen("MrBB1.out", "w", stdout);
	scanf("%d", &N);
	for (int i = 1; i <= N; i++) {
		scanf("%d", &p[i]);
		now += abs(p[i] - i);
		if (p[i]>=i)l++, sum[p[i] - i]++;
		else r++;
	}
	if (!now) { printf("0 0"); return 0; }
	ans = now; k = 0;
	//printf("%d ",now);
	//printf("%d %d\n",l,r);
	for (int i = 1; i<N; i++) {//当前进行第i次移位
		l -= sum[i-1]; r += sum[i-1];
		now = now - l + r  + abs(p[N - i + 1] - 1) - abs(p[N - i + 1] - N - 1);
		if (now<ans)ans = now, k = i;
		//printf("%d ",now);
		//printf("%d %d\n",l,r);
		//for (int j = 1; j <= N; j++)printf("%d ", sum[j]);
		sum[p[N - i + 1]+i-1]++; l++; r--;
	}
	printf("%lld %d",ans,k);
	return 0;
}