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\n");
                }
                else
                {
                    printf("%d\n",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函数,学习了!!!
*/

原文地址:https://www.cnblogs.com/yinbiao/p/9788263.html

时间: 2024-08-28 19:05:44

ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)的相关文章

ZOJ - 4016 Mergeable Stack (STL 双向链表)

[传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) 1 s v  即 s.push(v) (2) 2 s 即 s.pop() 输出弹出的元素,如果栈s为空则输出 "EMPTY" (3) 3 s t 把t栈元素全部移到s栈中,使s的尾部与t的首部相连. 现在有若干上述三种类型的操作,遇到操作2则输出相应内容. [题解]由于站的数量n和操作次数

ZOJ - 4016 Mergeable Stack 【链表合并】

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&q

[ZOJ 4016] Mergable Stack

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 直接用栈爆内存,看网上大神用数组实现的,构思巧妙,学习了! AC代码: /* * 用数组实现栈的一些操作 */ #include <cstdio> #include <cstring> using namespace std; const int maxn = 300005; int arr[maxn]; //存放所有的数据 //top代表栈

UVA10205 - Stack &#39;em Up(模拟)

UVA10205 - Stack 'em Up(模拟) 题目链接 题目大意:给你52张牌,这些牌一开始就有个顺序.现在给你每次的洗牌动作,52个数Ai,表示第i个位置上的牌放到Ai位置.意思就是能够通过这次洗牌,可以将i位置上的牌放到Ai位置上.至于后面的牌要不要移动什么的,根本不考虑.反正就是通过这次的洗牌,我给你52个数,把每个位置上的牌更新了一下. 解题思路:之前的题意看错,还以为是每次洗牌然后插入,后面才发现题意是这样的.那么只需要记录下上次洗牌的每个位置的牌,然后在将这次洗牌动作后的每

ZOJ 3210 A Stack or A Queue? (I)

A Stack or A Queue? Time Limit: 1 Second      Memory Limit: 32768 KB Do you know stack and queue? They're both important data structures. A stack is a "first in last out" (FILO) data structure and a queue is a "first in first out" (FIF

ZOJ 3804 YY&#39;s Minions (简单模拟)

1 /* 2 题意:一个矩阵中有 n*m个宠物,每一个宠物都有一个状态, 1醒着的,0睡着的 3 X离开的!如果这个宠物(醒着的)的周围醒着的个数>3 || <2它就会睡着, 4 如果这个宠物(睡着的)的周围醒着的个数==3就会醒来! 5 每一分钟都会有变换一个状态! 6 其中会有些宠物会在给定的时间内离开! 7 */ 8 #include<iostream> 9 #include<cstring> 10 #include<cstdio> 11 #inclu

利用反射模拟一个spring的内部工作原理

这个简单的案例是实行了登录和注册的功能,没有链接数据库. 在bean中id 是唯一的,id和name的区别在于id不能用特殊字符而name可以用特殊字符,比如:/-\.... 1 package com.obtk.reflect; 2 3 public class Logon { 4 /** 5 * 帐号密码长度大于六位字符就成功,否则失败! 6 * */ 7 public String select(String name, String pass) { 8 if (name.length()

zoj 3210 A Stack or A Queue? (数据结构水题)

 A Stack or A Queue? Time Limit: 1 Second      Memory Limit: 32768 KB Do you know stack and queue? They're both important data structures. A stack is a "first in last out" (FILO) data structure and a queue is a "first in first out" (

利用phantomjs模拟QQ自动登录

之前为了抓取兴趣部落里的数据,研究了下QQ自动登录. 当时搜索了一番,发现大部分方法都已经失效了,于是准备自己开搞. 第一个想到的就是参考网上已有方案的做法,梳理登陆js的实现,通过其他语言重写.考虑到js可能会更新,放弃了此方案. 第二个想到的是能不能直接调用QQ自己的js方法,模拟进行提交呢.搜索一番后发现神器 ---- "phantomjs". PhantomJS is a headless WebKit scriptable with a JavaScript API. It