zoukankan      html  css  js  c++  java
  • ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)

    Mergeable Stack

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Given initially empty stacks, there are three types of operations:

    • 1 s v: Push the value onto the top of the -th stack.

    • 2 s: Pop the topmost value out of the -th stack, and print that value. If the -th stack is empty, pop nothing and print "EMPTY" (without quotes) instead.

    • 3 s t: Move every element in the -th stack onto the top of the -th stack in order.

      Precisely speaking, denote the original size of the -th stack by , and the original size of the -th stack by . Denote the original elements in the -th stack from bottom to top by , and the original elements in the -th stack from bottom to top by .

      After this operation, the -th stack is emptied, and the elements in the -th stack from bottom to top becomes . Of course, if , this operation actually does nothing.

    There are operations in total. Please finish these operations in the input order and print the answer for every operation of the second type.

    Input

    There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

    The first line contains two integers and ( ), indicating the number of stacks and the number of operations.

    The first integer of the following lines will be ( ), indicating the type of operation.

    • If , two integers and ( , ) follow, indicating an operation of the first type.
    • If , one integer ( ) follows, indicating an operation of the second type.
    • If , two integers and ( , ) follow, indicating an operation of the third type.

    It's guaranteed that neither the sum of nor the sum of over all test cases will exceed .

    Output

    For each operation of the second type output one line, indicating the answer.

    Sample Input

    2
    2 15
    1 1 10
    1 1 11
    1 2 12
    1 2 13
    3 1 2
    1 2 14
    2 1
    2 1
    2 1
    2 1
    2 1
    3 2 1
    2 2
    2 2
    2 2
    3 7
    3 1 2
    3 1 3
    3 2 1
    2 1
    2 2
    2 3
    2 3
    

    Sample Output

    13
    12
    11
    10
    EMPTY
    14
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    

    Author: WENG, Caizhi
    Source: The 18th Zhejiang University Programming Contest Sponsored by TuSimple

    分析:

    /*
    有n个栈,q次操作
    1 s t:将t压入第s个栈
    2 s:第s个栈pop一个元素并打印
    3 s t:栈t从底到顶压入s栈,并将t栈清空
    注意用list模拟栈的操作,特别是栈的合并操作,采用的是splice函数,学习了!!!
    */
     
    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    #include<string.h>
    #include<set>
    #include<map>
    #include<list>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    
    list<int> li[300005];
    int main()
    {
        int t,n,q,op;
        int index1,index2,v;
        cin>>t;
        while(t--)
        {
            scanf("%d %d",&n,&q);
            for(int i=0;i<=n;i++)
                li[i].clear();
            while(q--)
            {
                scanf("%d",&op);
                if(op==1)
                {
                    scanf("%d %d",&index1,&v);
                    li[index1].push_back(v);
                }else if(op==2)
                {
                    scanf("%d",&index1);
                    if(li[index1].empty())
                    {
                        printf("EMPTY
    ");
                    }
                    else
                    {
                        printf("%d
    ",li[index1].back());
                        li[index1].pop_back();
                    }
                }else if(op==3)
                {
                    scanf("%d %d",&index1,&index2);
                    li[index1].splice(li[index1].end(),li[index2]);
                }
            }
        }
        return 0;
    }
    /*
    有n个栈,q次操作
    1 s t:将t压入第s个栈
    2 s:第s个栈pop一个元素并打印
    3 s t:栈t从底到顶压入s栈,并将t栈清空
    
    注意用list模拟栈的操作,特别是栈的合并操作,采用的是splice函数,学习了!!!
    */
  • 相关阅读:
    跨域上传图片的尝试过程,最终成功了。哈哈
    老子再也不加什么所谓的技术群了,顶撞群主的话,就被踢了。
    开发使用的插件
    设计原则记录
    程序员修神之路--redis做分布式锁可能不那么简单
    程序员过关斩将--面试官再问你Http请求过程,怼回去!
    程序员修神之路--问世间异步为何物?
    程序员修神之路--提高网站的吞吐量
    程序员过关斩将--你的业务是可变的吗(福利你领了吗)
    程序员修神之路--🤠分布式高并发下Actor模型如此优秀🤠
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9788263.html
Copyright © 2011-2022 走看看