比赛 Mister B的奇妙生活 评测结果 TATAAAAAAA
题目名称 B先生和PR移位 最终得分 80
用户昵称 delta_saberlover 运行时间 2.776 s
代码语言 C++ 内存使用 4.13 MiB
提交时间 2017-09-08 20:40:05
显示代码纯文本
#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;
}