【WikiOI】【P1743】【反转卡片】【题解】【平衡树or rope】

传送门:www.wikioi.com/problem/1743/

康复计划第二弹,本来想练练Treap|Splay的,看到这么水的题直接rope秒杀

Code:

//ID:zky
#include<cstdio>
#include<cctype>
#include<ext/rope>
#include<iostream>
#include<algorithm>
using namespace std;
using namespace __gnu_cxx;
const int maxn=300010;
rope<int>s1,s2,tmp1,tmp2;
int n;
int a[maxn];
int getint(){
	int res=0,ok=0;char ch;
	while(1){
		ch=getchar();
		if(isdigit(ch)){
			res*=10;res+=ch-'0';ok=1;
		}else if(ok)break;
	}return res;
}
void rev(int size){
	tmp1=s1.substr(0,size);
	tmp2=s2.substr(n-size,size);
	s1=tmp2+s1.substr(size,n-size);
	s2=s2.substr(0,n-size)+tmp1;
}
int main(){
	n=getint();for(int i=0;i<n;i++)a[i]=getint();
	for(int i=0;i<n;i++)s1.push_back(a[i]);
	for(int i=n-1;i>=0;i--)s2.push_back(a[i]);
	int ans=0;
	while(s1[0]!=1){
		rev(s1[0]);
		ans++;
		if(ans>100000){
			puts("-1");return 0;
		}
	}
	cout<<ans<<endl;
	return 0;
}


你可能感兴趣的:(省选)