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

    题意 t组样例 每组 n个栈 q个操作 操作1 将 val 放到第i个栈顶
                     操作2 输出 第 i 个栈的栈顶 弹出栈顶 为空 输出EMPTY
                     操作3 将 第 j个栈 整个栈 放到第i个栈的栈顶

    解析 用栈的话会MLE TLE 要用到c++容器 list 操作三就是o(1)复杂度 内存也没有问题

    AC代码
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <string>
    #include <queue>
    #include <map>
    #include <vector>
    #include <set>
    #include <utility>
    #include <stack>
    #include <list>
    using namespace std;
    const int maxn = 3e5+50,inf = 0x3f3f3f3f, mod = 998244353;
    const double epx = 1e-6;
    const double PI = acos(-1.0);
    typedef long long ll;
    list<int> l[maxn];
    int main()
    {
        int t,n,q;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d %d",&n,&q);
            for(int i=0;i<=n;i++)
                l[i].clear();
            while(q--)
            {
                int m,i,j,val;
                scanf("%d",&m);
                if(m==1)
                {
                    scanf("%d%d",&i,&val);
                    l[i].push_back(val);
                }
                else if(m==2)
                {
                    scanf("%d",&i);
                    if(l[i].empty())
                        printf("EMPTY
    ");
                    else
                    {
                        printf("%d
    ",l[i].back());
                        l[i].pop_back();
                    }
                }
                else
                {
                    scanf("%d%d",&i,&j);
                    l[i].splice(l[i].end(),l[j]);
                }
            }
        }
    }

     博客推荐

     https://blog.csdn.net/gscsdlz/article/details/52130225

    https://blog.csdn.net/gogokongyin/article/details/51178378

  • 相关阅读:
    iOS开发Quzrtz2D 十:圆形图片的绘制以及加边框圆形图片的绘制
    团队项目(周日站立会议)
    团队项目(周六站立会议)
    团队项目(spring会议)
    团队项目(第一次会议)
    结对开发项目 电梯调度发布版(已完成) 刘佳琪、兰梦
    结对测试二(求二维数组的子数组之和最大值)(兰梦、刘佳琪)
    敏捷开发(对于敏捷开发模式的理解)
    上课结对测试项目(求整数数组的子数组之和的最大值)(兰梦,刘佳琪)
    结对开发项目:多部电梯调度(一)(兰梦、刘佳琪)
  • 原文地址:https://www.cnblogs.com/stranger-/p/8763015.html
Copyright © 2011-2022 走看看