比赛 |
Mister B的奇妙生活 |
评测结果 |
WWWWAAAAAA |
题目名称 |
B先生和PR移位 |
最终得分 |
60 |
用户昵称 |
AAAAAAAAAA |
运行时间 |
0.367 s |
代码语言 |
C++ |
内存使用 |
15.55 MiB |
提交时间 |
2017-09-08 20:42:22 |
显示代码纯文本
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cmath>
#define MAXN 2000010
#define INF 0x7fffffff
using namespace std;
int N, p[MAXN], l, r, now, ans, 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("%d %d",ans,k);
return 0;
}