显示代码纯文本
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<cassert>
using namespace std;
typedef long long LL;
/*LL abs(LL a){
return a>0?a:-a;
}*/
typedef multiset<int> ST;
const int SIZEN=1000010;
const LL INF=1e15;
int N;
int P[SIZEN]={0};
LL ans=INF,best=-1;
void update(LL now,LL k){
if(now<ans){
ans=now;
best=k;
}
}
ST S;LL posnum=0,nonposnum=0;
LL lazy;//at any time, item x in S is actually a+lazy
LL now=0;
void insert(LL x){//insert real value x
now+=abs(x);
if(x>0) posnum++;
else nonposnum++;
S.insert(x-lazy);
}
void erase(LL x){//erase real value x
ST::iterator key=S.find(x-lazy);
assert(key!=S.end());
now-=abs(x);
if(x>0) posnum--;
else nonposnum--;
S.erase(key);
}
void total_dec(void){
now-=posnum;
now+=nonposnum;
lazy--;
int m=S.count(-lazy);//count number of 0s
posnum-=m;
nonposnum+=m;
}
void work(void){
//for an operation, everything -1
LL lazy=0;
for(int i=1;i<=N;i++) insert(P[i]-i);
update(now,0);
for(int i=1;i<=N-1;i++){
int t=N+1-i;//P[t]-N change to P[t]-1
erase(P[t]-N);
total_dec();
insert(P[t]-1);
update(now,i);
}
cout<<ans<<" "<<best<<endl;
}
void read(void){
scanf("%d",&N);
for(int i=1;i<=N;i++) scanf("%d",&P[i]);
}
int main(){
freopen("MrBB1.in","r",stdin);
freopen("MrBB1.out","w",stdout);
read();
work();
return 0;
}