显示代码纯文本
#include<fstream>
#include<cmath>
#include<algorithm>
using namespace std;
ifstream fin("MrBB1.in");
ofstream fout("MrBB1.out");
int n,a[1000005],f[1000005],k;
int L,R;
long long ans,m;
int main()
{
fin>>n;
int i,j,k;
for(i = 1;i <= n;i++)
fin >> a[i];
for(i = 1;i <= n;i++)
{
m+= abs(a[i]-i);
if(a[i] >= i)
{
L++;
f[a[i]-i]++;
}
else
{
R++;
}
//b[i] = a[i];
}
ans = m;
for(i = 0;i < n-1;i++)
{
L -= f[i];
R += f[i];
m = m-L+R-abs(a[n-i]-n-1)+a[n-i]-1;
f[a[n-i]+i]++;
L++;
R--;
if(m< ans)
{
ans = m;
k = i+1;
}
}
fout << ans << " " <<k<< endl;
fin.close();
fout.close();
return 0;
}