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栈清空
*/
#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[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]={{,},{,-},{,},{-,}}; list<int> li[];
int main()
{
int t,n,q,op;
int index1,index2,v;
cin>>t;
while(t--)
{
scanf("%d %d",&n,&q);
for(int i=;i<=n;i++)
li[i].clear();
while(q--)
{
scanf("%d",&op);
if(op==)
{
scanf("%d %d",&index1,&v);
li[index1].push_back(v);
}else if(op==)
{
scanf("%d",&index1);
if(li[index1].empty())
{
printf("EMPTY\n");
}
else
{
printf("%d\n",li[index1].back());
li[index1].pop_back();
}
}else if(op==)
{
scanf("%d %d",&index1,&index2);
li[index1].splice(li[index1].end(),li[index2]);
}
}
}
return ;
}
/*
有n个栈,q次操作
1 s t:将t压入第s个栈
2 s:第s个栈pop一个元素并打印
3 s t:栈t从底到顶压入s栈,并将t栈清空 注意用list模拟栈的操作,特别是栈的合并操作,采用的是splice函数,学习了!!!
*/
ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)的更多相关文章
- ZOJ 4016 Mergeable Stack(栈的数组实现)
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, the ...
- ZOJ 4016 Mergeable Stack 链表
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, the ...
- ZOJ - 4016 Mergeable Stack 【LIST】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...
- ZOJ - 4016 Mergeable Stack (STL 双向链表)
[传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) ...
- ZOJ 4016 Mergeable Stack(from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)
模拟题,用链表来进行模拟 # include <stdio.h> # include <stdlib.h> typedef struct node { int num; str ...
- Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))
F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [ZOJ 4016] Mergable Stack
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 直接用栈爆内存,看网上大神用数组实现的,构思巧妙,学习了! ...
- 利用正则表达式模拟计算器进行字符串的计算实现eval()内置函数功能
代码感觉有点绕,刚开始学习python,相关知识点还没全部学习到,还请各位大神多多指教 import re # 定义乘法 def mul(string): mul1 = re.search('-?\d ...
- Mergeable Stack ZOJ - 4016(list)
ZOJ - 4016 vector又T又M list是以链表的方式存储的 是一个双向链表 元素转移操作中,不能一个个遍历加入到s中,list独有的splic函数可以在常数时间内实现合并(并删除源lis ...
随机推荐
- springboot项目中js、css静态文件路径访问
springboot静态文件访问的问题,相信大家也有遇到这个问题,如下图项目结构. 项目结构如上所示,静态页面引入js.css如下所示. 大家肯定都是这样写的,但是运行的话就是出不来效果,图片也不显示 ...
- css BFC布局及用处
http://www.cnblogs.com/lhb25/p/inside-block-formatting-ontext.html 这篇文章讲的很简单很实用
- 原生JSON解析
原生JSON解析 JSONObject:JSON数据封装对象JSONArray:JSON数据封装数组 布局: <?xml version="1.0" encoding=&qu ...
- 安卓逆向(一)--Smali基础
安卓逆向(一)--Smali基础 标签(空格分隔): 安卓逆向 APK的组成 文件夹 作用 asset文件夹 资源目录1:asset和res都是资源目录但有所区别,见下面说明 lib文件夹 so库存放 ...
- 在Android Studio中调用so中的方法
本节用的so是上节用Android Studio创建的so.想在Android Studio中调用so中的方法,需要先引用so.Android Studio中引用so的方法有二种,下面开始介绍. 一 ...
- 使用ES6+Vue+webpack+gulp构建新一代Web应用
1.推荐学习网站:Vue.js中国 2.Demo环境搭建: 2.1环境配置 安装nodejs环境,具体内容可以百度: 新建一个文件夹: mkdir VUE-ES6-WebPack 全局安装gulp: ...
- UIAutomator环境搭建
目录 下载.安装JDK&配置Java环境变量 下载.安装SDK.ADT&配置Android环境变量 下载.安装ANT&配置ANT环境变量 创建UIAutomator工程 UIA ...
- C#多线程的用法8-线程间的协作AutoResetEvent
AutoResetEvent自动重置事件,与ManualResetEvent是相对的而言.它同样用于线程间同步,请对照<C#多线程的用法7-线程间的协作ManualResetEvent>进 ...
- leveldb源码分析--Comparator
既然leveldb是一个按Key序组织的LSM-Tree实现,那么对于Key的比较就是非常之重要了,这个Key的比较在leveldb中是Comparator的形式出现的.我们首先来看看Comparat ...
- Oracle EBS 用户职责人员取值
SELECT fu.user_name 用户名, fu.description 用户说明, fu.start_date 用户启用日期, fu.end_date 用户终止日期 --,fu.employe ...