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;
}

#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;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

We Tested Viral Kitchen Gadgets Here Is What You Must Know

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Lane Kiffin reveals how NIL impacts player motivation, says LSU has ‘evolved’ in new era

College football has undergone a seismic shift over the past few years, evolving from a strictly amateur framework into a complex landscape defined by...

Is Viral Kitchen Gadgets Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!