显示代码纯文本
#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;
}