链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there are always lots of randomness in the game. For most of the cards game, the very first step in the game is shuffling the cards. And, mostly the randomness in the game is from this step. However, Eddy doubts that if the shuffling is not done well, the order of the cards is predictable! To prove that, Eddy wants to shuffle cards and tries to predict the final order of the cards. Actually, Eddy knows only one way to shuffle cards that is taking some middle consecutive cards and put them on the top of rest. When shuffling cards, Eddy just keeps repeating this procedure. After several rounds, Eddy has lost the track of the order of cards and believes that the assumption he made is wrong. As Eddy's friend, you are watching him doing such foolish thing and easily memorizes all the moves he done. Now, you are going to tell Eddy the final order of cards as a magic to surprise him. Eddy has showed you at first that the cards are number from 1 to N from top to bottom. For example, there are 5 cards and Eddy has done 1 shuffling. He takes out 2-nd card from top to 4-th card from top(indexed from 1) and put them on the top of rest cards. Then, the final order of cards from top will be [2,3,4,1,5]. 输入描述: The first line contains two space-separated integer N, M indicating the number of cards and the number of shuffling Eddy has done. Each of following M lines contains two space-separated integer pi, si indicating that Eddy takes pi-th card from top to (pi+si-1)-th card from top(indexed from 1) and put them on the top of rest cards. 1 ≤ N, M ≤ 105 1 ≤ pi ≤ N 1 ≤ si ≤ N-pi+1 输出描述: Output one line contains N space-separated integers indicating the final order of the cards from top to bottom. 示例1 输入 复制 5 1 2 3 输出 复制 2 3 4 1 5 示例2 输入 复制 5 2 2 3 2 3 输出 复制 3 4 1 2 5 示例3 输入 复制 5 3 2 3 1 4 2 4 输出 复制 3 4 1 5 2
#include<bits/stdc++.h> #include<ext/rope> //固定写法 using namespace std; using namespace __gnu_cxx; //固定写法 rope<int> s; //实质是可持久化平衡树 int main() { int n,m,l,e,i; scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ s.push_back(i); //放元素 } while(m--){ scanf("%d%d",&l,&e); s=s.substr(l-1,e)+s.substr(0,l-1)+s.substr(l+e-1,n-e-(l-1)); //将区间放置首位,重新组合数组,substr(起始字符,元素个数) } for(i=0;i<s.size();i++){ if(i>0) printf(" "); printf("%d",s[i]); //元素按下标输出 } return 0; }
平衡树:他是区间旋转。你可以通过旋转3次得倒。
#include<iostream> #include<stdio.h> using namespace std; int n,m,sz,rt; int fa[100005],c[100005][2],id[100005]; int size[100005]; bool rev[100005]; void pushup(int k) { int l=c[k][0],r=c[k][1]; size[k]=size[l]+size[r]+1; } void pushdown(int k) { int l=c[k][0],r=c[k][1]; if(rev[k]) { swap(c[k][0],c[k][1]); rev[l]^=1;rev[r]^=1; rev[k]=0; } } void rotate(int x,int &k) { int y=fa[x],z=fa[y],l,r; if(c[y][0]==x)l=0;else l=1;r=l^1; if(y==k)k=x; else {if(c[z][0]==y)c[z][0]=x;else c[z][1]=x;} fa[x]=z;fa[y]=x;fa[c[x][r]]=y; c[y][l]=c[x][r];c[x][r]=y; pushup(y);pushup(x); } void splay(int x,int &k) { while(x!=k) { int y=fa[x],z=fa[y]; if(y!=k) { if(c[y][0]==x^c[z][0]==y)rotate(x,k); else rotate(y,k); } rotate(x,k); } } int find(int k,int rank) { pushdown(k); int l=c[k][0],r=c[k][1]; if(size[l]+1==rank)return k; else if(size[l]>=rank)return find(l,rank); else return find(r,rank-size[l]-1); } void rever(int l,int r) { int x=find(rt,l),y=find(rt,r+2); splay(x,rt);splay(y,c[x][1]); int z=c[y][0]; rev[z]^=1; } void build(int l,int r,int f) { if(l>r)return; int now=id[l],last=id[f]; if(l==r) { fa[now]=last;size[now]=1; if(l<f)c[last][0]=now; else c[last][1]=now; return; } int mid=(l+r)>>1;now=id[mid]; build(l,mid-1,mid);build(mid+1,r,mid); fa[now]=last;pushup(mid); if(mid<f)c[last][0]=now; else c[last][1]=now; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n+2;i++) id[i]=++sz; build(1,n+2,0);rt=(n+3)>>1; for(int i=1;i<=m;i++) { int l,r; scanf("%d%d",&l,&r); rever(1,l+r-1); rever(1,r); rever(r+1,l+r-1); } for(int i=2;i<=n+1;i++) printf("%d ",find(rt,i)-1); return 0; }