BZOJ 1552: [Cerc2007]robotic sort
来源:互联网 发布:apache 开源协议 编辑:程序博客网 时间:2024/06/06 09:01
1552: [Cerc2007]robotic sort
Time Limit: 5 Sec Memory Limit: 64 MB
Description
Input
输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000。
第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号。
Output
输出共一行,N个用空格隔开的正整数P1,P2,P3…Pn,Pi表示第i次操作前第i小的物品所在的位置。
注意:如果第i次操作前,第i小的物品己经在正确的位置Pi上,我们将区间[Pi,Pi]反转(单个物品)。
Sample Input
6
3 4 5 1 6 2
Sample Output
4 6 4 5 6 6
HINT
Source
HNOI2009集训Day6
//以权值为下标 #include<iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;#define MAXN 100010#define INF 0x7fffffffinline int read(int &x){ char c=getchar();int f=1;x=0; while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+(c-'0');c=getchar();} x*=f;}struct Data{ int data,pos; }a[MAXN];int rev[MAXN],ch[MAXN][2],Min[MAXN],Min_Pos[MAXN],data[MAXN],fa[MAXN];int siz[MAXN],n,root;bool cmp1(Data a,Data b){ if(a.data==b.data) return a.pos<b.pos; return a.data<b.data;}inline void Push_Down(int x){ if(rev[x]){ rev[x]=0; rev[ch[x][0]]^=1; swap(ch[ch[x][0]][1],ch[ch[x][0]][0]); rev[ch[x][1]]^=1; swap(ch[ch[x][1]][1],ch[ch[x][1]][0]); }}inline void UpDate(int x){ Min[x]=min(data[x],min(Min[ch[x][0]],Min[ch[x][1]]));//左子树、右子树、自身 if(Min[x]==data[x]) Min_Pos[x]=x; else if(Min[x]==Min[ch[x][0]]) Min_Pos[x]=Min_Pos[ch[x][0]]; else Min_Pos[x]=Min_Pos[ch[x][1]]; siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;}inline int getson(int x){ return ch[fa[x]][1]==x;}/*inline void Rotate(int x){ int y=fa[x],z=fa[fa[x]],k=getson(x); ch[y][k]=ch[x][k^1];fa[ch[y][k]]=y; ch[x][k^1]=y;fa[y]=x;fa[x]=z; if(z) ch[z][ch[z][1]==y]=x; UpDate(y);UpDate(x);}*/inline void Rotate(int x){ int y=fa[x],z=fa[y],b=getson(x),c=getson(y),a=ch[x][!b]; if(z) ch[z][c]=x; else root=x; fa[x]=z; if(a) fa[a]=y; ch[y][b]=a; ch[x][!b]=y;fa[y]=x; UpDate(y);UpDate(x);}void Splay(int &x,int i){ while(fa[x]!=i){ int y=fa[x],z=fa[y]; if(z==i) Rotate(x); else { Push_Down(z);Push_Down(y);Push_Down(x); if(getson(x)==getson(y)) Rotate(y),Rotate(x); else Rotate(x),Rotate(x); } }}inline int Get_Kth(int k,int x){ Push_Down(x); if(k==siz[ch[x][0]]+1) return x; if(k<siz[ch[x][0]]+1) return Get_Kth(k,ch[x][0]); return Get_Kth(k-1-siz[ch[x][0]],ch[x][1]);}inline void reverse(int l,int r){ int ll=Get_Kth(l-1,root),rr=Get_Kth(r+1,root),p; Splay(ll,0);Splay(rr,ll); p=ch[rr][0];rev[p]^=1; swap(ch[p][0],ch[p][1]);}inline int Get_Min_Pos(int l,int r){ int ll=Get_Kth(l-1,root);// int rr=Get_Kth(r+1,root);// Splay(ll,0);//转到根 Splay(rr,ll);//根的左儿子 return Min_Pos[ch[rr][0]];}int main(){ read(n); for(int i=2;i<=n+1;i++){ read(data[i]); a[i].data=data[i]; a[i].pos=i; } sort(a+2,a+n+2,cmp1); for(int i=2;i<=n+1;i++) data[a[i].pos]=i; for(int i=0;i<=n+2;i++) Min[i]=INF; data[0]=INF;data[1]=INF;data[n+2]=INF; root=1;siz[0]=0; for(int i=1;i<=n+2;i++) fa[i]=i-1,ch[i][1]=i+1; ch[n+2][1]=0; for(int i=n+2;i>=1;i--) UpDate(i); for(int i=1;i<=n;i++){ int p=Get_Min_Pos(i+1,n+1);//找到最小的 Splay(p,0);//最小的转到根 printf("%d",siz[ch[p][0]]);//位置 reverse(i+1,siz[ch[p][0]]+1);//操作--翻转 if(i!=n) printf(" "); } printf("\n"); return 0;}
阅读全文
0 0
- 【BZOJ 1552】[Cerc2007]robotic sort
- BZOJ 1552 [Cerc2007]robotic sort
- BZOJ 1552 [Cerc2007]robotic sort
- BZOJ 1552: [Cerc2007]robotic sort
- bzoj 1552: [Cerc2007]robotic sort(splay)
- bzoj【1552/3506】[Cerc2007]robotic sort
- BZOJ1552 [Cerc2007]robotic sort
- bzoj 1552: [Cerc2007]robotic sort && bzoj 3506: [Cqoi2014]排序机械臂(splay区间翻转)
- BZOJ 1552: [Cerc2007]robotic sort/3506: [Cqoi2014]排序机械臂 splay
- 【bzoj1552】[Cerc2007]robotic sort splay
- 【bzoj1552/3506】[Cerc2007]robotic sort
- [BZOJ1552][Cerc2007]robotic sort(splay)
- bzoj3506&1552 [Cerc2007][Cqoi2014]robotic sort 排序机械臂(splay)
- Splay 模板 BZOJ 1552/3506 robotic sort
- bzoj3506【CQOI2014】排序机械臂 bzoj1552【CERC2007】robotic sort
- BZOJ 3506 robotic sort (splay)
- 【bzoj3506/1552】【robotic sort】【splay】
- hdu 1089 Robotic Sort
- IDEA Maven-Helper插件
- Zedboard中的SPI通信记录文档(已实现)
- 定时清理过期的topic和queue
- 51Nod-1530-稳定方块
- js中的通用事件
- BZOJ 1552: [Cerc2007]robotic sort
- HDU1232 畅通工程 并查集入门
- nginx的安装和配置
- HttpClient4.5 基础教程 执行请求<三>
- leetCode 41.First Missing Positive (第一个丢掉的正数) 解题思路和方法
- B
- HDU6047优先队列+贪心
- 【ML--06】主成分分析PCA
- win10 激活