zoukankan      html  css  js  c++  java
  • ZOJ 4016 Mergeable Stack(栈的数组实现)

    Mergeable Stack

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

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

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

    • 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

    #include<bits/stdc++.h>  
    using namespace std;  
    const int maxn=300005;  
    int a[maxn],top[maxn],bottom[maxn],Next[maxn];  
    int cnt;  
    void Push(int s,int v)  
    {  
        a[++cnt]=v;  
        if(bottom[s]==0)  
        {  
            bottom[s]=cnt;  
        }  
        Next[cnt]=top[s];  
        top[s]=cnt;  
    }  
    void Pop(int s)  
    {  
        if(top[s]==0)printf("EMPTY
    ");  
        else  
        {  
            printf("%d
    ",a[top[s]]);  
            top[s]=Next[top[s]];  
            if(top[s]==0)bottom[s]=0;  
        }  
    }  
    void swapp(int s,int v)  
    {  
        if(bottom[s]==0)bottom[s]=bottom[v];  
        Next[bottom[v]]=top[s];  
        top[s]=top[v];  
        bottom[v]=top[v]=0;  
    }  
    int main()  
    {  
        int T;scanf("%d",&T);  
        while(T--)  
        {  
            memset(bottom,0,sizeof(bottom));  
            memset(top,0,sizeof(top));  
            memset(Next,0,sizeof(Next));  
            int n,Q;scanf("%d%d",&n,&Q);  
            cnt=0;  
            while(Q--)  
            {  
                int op,x,y;scanf("%d",&op);  
                if(op==1)  
                {  
                    scanf("%d%d",&x,&y);  
                    Push(x,y);  
                }  
                else if(op==2)  
                {  
                    scanf("%d",&x);  
                    Pop(x);  
                }  
                else  
                {  
                    scanf("%d%d",&x,&y);  
                    if(bottom[y]==0)continue;  
                    swapp(x,y);  
                }  
            }  
        }  
        return 0;  
    }  
  • 相关阅读:
    Java59道常见面试题,内附答案
    2020年一个月时间面试字节跳动,面试经历分享(已拿offer)
    阿里总结的《Java成神之路》 PDF 火了,完整版开放下载!
    Github14k堪称神级的Spring Boot手册,从基础入门到实战进阶
    三面阿里(支付宝)Java高开岗,复习两月有幸拿到offer
    Java 最常见的 200+ 面试题:面试必备
    【面试总结】终于拿到了美团offer了,没有辜负了这三个月的努力啊
    阿里P9都赞不绝口的面试宝典!半月看完25大专题,居然斩获阿里P7offer
    Integer是int
    抽象类和接口
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8847112.html
Copyright © 2011-2022 走看看