zoukankan      html  css  js  c++  java
  • 2018牛客网暑期ACM多校训练营(第三场) H

    题目链接:https://www.nowcoder.com/acm/contest/141/C

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 262144K,其他语言524288K
    Special Judge, 64bit IO Format: %lld

    题目描述

    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].

    输入描述:

    输出描述:

    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

    题意:

    给出一个1~n的数字序列,给出m次操作,每次操作会将以第p个数字为开始的连续s个数字拿出来放到整个序列的开头,求m次操作完成后的序列。

    题解:

    用splay来搞区间问题:https://www.cnblogs.com/dilthey/p/9379652.html#splay-5

    可以直接先区间移动的函数,也可以使用三次区间反转来完成区间移动。

    AC代码:

    #include<bits/stdc++.h>
    #define Key_value ch[ch[root][1]][0]
    using namespace std;
    const int maxn=1e5+10;
    
    int n,m;
    
    /******************************** splay - st ********************************/
    int root,nodecnt;
    int par[maxn],ch[maxn][2];
    int key[maxn],size[maxn];
    bool rev[maxn];
    void NewNode(int &x,int p,int k)
    {
        x=++nodecnt;
        par[x]=p;
        ch[x][0]=ch[x][1]=0;
        key[x]=k;
        size[x]=1;
        rev[x]=0;
    }
    void Update_Rev(int x)
    {
        if(x==0) return;
        swap(ch[x][0],ch[x][1]);
        rev[x]^=1;
    }
    void Pushup(int x)
    {
        size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
    }
    void Pushdown(int x)
    {
        if(rev[x])
        {
            Update_Rev(ch[x][0]);
            Update_Rev(ch[x][1]);
            rev[x]=0;
        }
    }
    void Rotate(int x,int type) //旋转,0为左旋zag,1为右旋zig
    {
        int y=par[x];
        Pushdown(y); Pushdown(x); //先把y的标记向下传递,再把x的标记往下传递
        ch[y][!type]=ch[x][type]; par[ch[x][type]]=y;
        if(par[y]) ch[par[y]][(ch[par[y]][1]==y)]=x;
        par[x]=par[y];
        ch[x][type]=y; par[y]=x;
        Pushup(y); Pushup(x);
    }
    void Splay(int x,int goal)
    {
        while(par[x]!=goal)
        {
            if(par[par[x]]==goal) Rotate(x,ch[par[x]][0]==x); //左孩子zig,有孩子zag
            else
            {
                Pushdown(par[par[x]]); Pushdown(par[x]); Pushdown(x);
                int y=par[x];
                int type=(ch[par[y]][0]==y); //type=0,y是右孩子;type=1,y是左孩子
                if(ch[y][type]==x)
                {
                    Rotate(x,!type);
                    Rotate(x,type);
                }
                else
                {
                    Rotate(y,type);
                    Rotate(x,type);
                }
            }
        }
        if(goal==0) root=x;
    }
    int Get_Kth(int x,int k) //得到第k个节点
    {
        Pushdown(x);
        int t=size[ch[x][0]]+1;
        if(t==k) return x;
        if(t>k) return Get_Kth(ch[x][0],k);
        else return Get_Kth(ch[x][1],k-t);
    }
    int Get_Min(int x)
    {
        Pushdown(x);
        while(ch[x][0])
        {
            x=ch[x][0];
            Pushdown(x);
        }
        return x;
    }
    int Get_Max(int x)
    {
        Pushdown(x);
        while(ch[x][1])
        {
            x=ch[x][1];
            Pushdown(x);
        }
        return x;
    }
    void Build(int &x,int l,int r,int par) //建树,先建立中间结点,再建两端的方法
    {
        if(l>r) return;
        int mid=(l+r)/2;
        NewNode(x,par,mid);
        Build(ch[x][0],l,mid-1,x);
        Build(ch[x][1],mid+1,r,x);
        Pushup(x);
    }
    void Init() //初始化,前后各加一个空节点
    {
        root=nodecnt=0;
        ch[root][0]=ch[root][1]=size[root]=key[root]=par[root]=rev[root]=0;
        NewNode(root,0,-1); //头部加入一个空位
        NewNode(ch[root][1],root,-1); //尾部加入一个空位
        Build(Key_value,1,n,ch[root][1]);
        Pushup(ch[root][1]);
        Pushup(root);
    }
    void Move(int l,int r,int p) //截取[l,r]放到位置p之后
    {
        Splay(Get_Kth(root,l-1+1),0); //l的前驱l-1伸展到根
        Splay(Get_Kth(root,r+1+1),root); //r的后继r+1伸展到根的右孩子
        int tmp=Key_value; //Key_value=ch[ch[root][1]][0]所统领的子树即[l,r]
        Key_value=0; //剥离[l,r]子树
        Pushup(ch[root][1]); Pushup(root);
        Splay(Get_Kth(root,p+0+1),0); //p伸展到根
        Splay(Get_Kth(root,p+1+1),root); //p的后继p+1伸展到根的右孩子
        Key_value=tmp; par[Key_value]=ch[root][1]; //接上[l,r]子树
        Pushup(ch[root][1]); Pushup(root);
    }
    void Reverse(int l,int r) //反转[l,r]区间
    {
        Splay(Get_Kth(root,l-1+1),0);
        Splay(Get_Kth(root,r+1+1),root);
        Update_Rev(Key_value);
        Pushup(ch[root][1]);
        Pushup(root);
    }
    /******************************** splay - ed ********************************/
    
    int tot;
    void Inorder(int x)
    {
        if(x==0) return;
        Pushdown(x);
        Inorder(ch[x][0]);
        tot++;
        if(tot>=2 && tot<=n+1)
        {
            if(tot>2) printf(" ");
            printf("%d",key[x]);
        }
        Inorder(ch[x][1]);
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        Init();
    
        for(int i=1,st,ed;i<=m;i++)
        {
            scanf("%d%d",&st,&ed); ed=st+ed-1;
    
            if(i%2) Move(st,ed,0);
            else
            {
                Reverse(1,st-1);
                Reverse(st,ed);
                Reverse(1,ed);
            }
        }
    
        Inorder(root);
        printf("
    ");
    }
  • 相关阅读:
    Oracle sql的基本优化写法和思路。
    Linux的简单介绍和开发基本运维时候用到的命令
    Nginx的使用(反向代理,负载均衡)
    Mybatis传值为空需要配置JdbcType来解决吗?(XML文件不需要配置JdbcType)
    Mybatis Blob和String互转,实现文件上传等。
    Ckeditor上传图片返回的JS直接显示出来,未执行!!!
    学习中的错误——ubuntu 14.04 LTS 启动eclipse报错
    2016计算机大会后记——机器学习:发展与未来
    2016计算机大会后记——大数据时代的模式识别
    近期编程问题——epoll failed:bad file descriptor
  • 原文地址:https://www.cnblogs.com/dilthey/p/9380823.html
Copyright © 2011-2022 走看看