显示代码纯文本
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int n;
int p[100005],c[100005];
int main()
{
freopen("MrBB1.in","r",stdin);
freopen("MrBB1.out","w",stdout);
int i,j;
cin>>n;
for (i=1;i<=n;i++)
cin>>p[i];
int L=0,R=0;
long long r=0,ans;
for(int i=1;i<=n;i++)
{
r+=abs(p[i]-i);
c[(p[i]-i+n)%n]++;
if(p[i]<=i)
R++;
else L++;
}
ans=r;
int d=0;
for(int k=1;k<n;k++)
{
r-=L;
r+=R-1;
r+=p[(n-k)%n+1]-n+p[(n-k)%n+1]-1;
L-=c[k]-1;
R+=c[k]-1;
if(r<ans)
{
d=k;
ans=r;
}
}
cout<<ans<<" "<<d<<endl;
fclose(stdin);fclose(stdout);
}