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 ...
随机推荐
- socket 模拟 HTTP请求
一.socket介绍 网络由下往上分为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层.IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层.socket则是对TCP/I ...
- 设计模式(20)--State(状态模式)--行为型
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.模式定义: 状态模式,又称状态对象模式(Pattern of Objects for States),状态模式 ...
- 【代码笔记】iOS-removeFromSuper
代码: RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after ...
- 【读书笔记】iOS-网络-HTTP-URL结构
http://user:password@hostname:port/absolute-path?query. http: 协议 user:password@ 认证 hostname: 主机名 ...
- JS中String与Array的一些常用方法
真是恨透了这些类似于substring substr slice 要么长得像,要么就功能相近的方法... 1⃣️string 1.substring(start开始位置的索引,end结束位置索引) 截 ...
- Application_Start 多次启动问题
最近在重构一个项目,在重构过程中出现了Application_Start 多次启动的问题,查询资料说是应用程序池内的修改会导致这个问题,后来发现确实如此 因为在重构过程中,我将数据库文件(sqlite ...
- Mockjs 前端接口数据模拟
在前后端分离的项目中,通常需要启动一个后台服务器来配合前端项目的接口需求.Mockjs的作用是拦截ajax请求并模拟各种数据返回,让前端开发可以更加自由独立. 安装 npm install mockj ...
- Web Api通过文件流下载文件到本地实例
最近项目里面需要和C++的客户端互动,其中一个接口就是需要提供文件下载的接口,保证C++项目调用这个接口的时候能够正常下载文件到本地.参考了一下网上的代码,其原理就是读取服务器上指定路径的文件流,并将 ...
- 分布式事务实现-Percolator
Google为了解决网页索引的增量处理,以及维护数据表和索引表的一致性问题,基于BigTable实现了一个支持分布式事务的存储系统.这里重点讨论这个系统的分布式事务实现,不讨论percolator中为 ...
- 【SVN】Linux下svn搭建配置全过程——初学者轻松上手篇
版本控制主要用到的是git和svn,其中svn界面化使用操作简单,本篇简单介绍SVN搭建配置全过程. 1. 下载并安装 yum install subversion 查看版本 svnserve --v ...