题意:定义一个排列$p_{1\cdots n}$的“偏移量”$D=\sum _{i=1}^n\left|p_i-i\right|$
求它所有的轮换排列中偏移量最小的是多少,要求输出轮换序数
暴力就是求出每个轮换排列然后计算$D$,我们不妨换个视角,看看如何计算每个$p_k$对不同排列的$D$的贡献
设$d_i$是轮换序数为$i$的轮换排列的偏移量(轮换从右往左),当前处理到$p_k$
#1若$p_k\geq k$
对$0\leq i\leq k-1$,贡献为$p_k-k+i$
对$k\leq i\leq k+n-p_k$,贡献为$n+k-p_k-i$
对$k+n-p_k+1\leq i\leq n-1$,贡献为$p_k-n-k+i$
#2若$p_k\lt k$
对$0\leq i\leq k-p_k-1$,贡献为$k-p_k-i$
对$k-p_k\leq i\leq k-1$,贡献为$p_k-k+i$
对$k\leq i\leq n-1$,贡献为$n+k-p_k-i$
区间加和,还带关于$i$的线性的项,直接打两个标记(常数,$i$的系数),最后扫一遍即可
#include<stdio.h>
#define ll long long
int p[2000010];
ll dc[4000010],dx[4000010];
ll min(ll a,ll b){return a<b?a:b;}
int main(){
int n,i,pos;
ll del,cnt,ans;
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%d",p+i);
for(i=1;i<=n;i++){
if(p[i]>=i){
dc[0]+=(p[i]-i);
dc[i]-=(p[i]-i);
dx[0]++;
dx[i]--;
dc[i]+=(n+i-p[i]);
dc[i+n-p[i]+1]-=(n+i-p[i]);
dx[i]--;
dx[i+n-p[i]+1]++;
if(i+n-p[i]+1<=n-1){
dc[i+n-p[i]+1]+=(p[i]-n-i);
dx[i+n-p[i]+1]++;
}
}else{
dc[0]+=(i-p[i]);
dc[i-p[i]]-=(i-p[i]);
dx[0]--;
dx[i-p[i]]++;
dc[i-p[i]]+=(p[i]-i);
dc[i]-=(p[i]-i);
dx[i-p[i]]++;
dx[i]--;
if(i<=n-1){
dc[i]+=(n+i-p[i]);
dx[i]--;
}
}
}
del=cnt=0;
ans=9223372036854775807ll;
for(i=0;i<n;i++){
del+=dx[i];
cnt+=dc[i];
if(cnt+i*del<ans){
ans=cnt+i*del;
pos=i;
}
}
printf("%I64d ",ans);
if(pos==0)
putchar('0');
else
printf("%d",n-pos);
}