BZOJ 1552: [Cerc2007]robotic sort

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 0x7fffffff
inline 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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值