https://i0.wp.com/eblogarithm.com/wp-content/uploads/2017/08/Codeforces-Round-421-Div-2-problem-D-Mister-B-and-PR-Shifts-Solution-In-CC1.png?fit=640%2C360

Codeforces Round #421 (Div. 2), problem: (D) Mister B and PR Shifts Solution In C/C++

#include<stdio.h>
#include<stdlib.h>
#define LL long long
int p[1000005], cur[2000005];
int main(void)
{
LL ans, sum;
int n, L, r, i, temp;
scanf(“%d”, &n);
sum = L = r = temp = 0;
for(i=1;i<=n;i++)
scanf(“%d”, &p[i]);
for(i=1;i<=n;i++)
{
sum += abs(p[i]-i);
if(p[i]>=i) L++, cur[p[i]-i]++;
else r++;
}
ans = sum;
for(i=0;i<n-1;i++)
{
L -= cur[i]; r += cur[i];
sum = sum-L+r-abs(p[n-i]-n-1)+p[n-i]-1;
cur[p[n-i]+i]++;
L++, r–;
if(sum<ans)
ans = sum, temp = i+1;
}
printf(“%lld %d\n”, ans, temp);
return 0;
}

(Visited 20 times, 1 visits today)



There are no comments

Add yours

Leave a Reply

%d bloggers like this: