ZOJ 4016 Mergeable Stack(栈的数组实现)
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
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn],top[maxn],bottom[maxn],Next[maxn];
int cnt;
void Push(int s,int v)
{
a[++cnt]=v;
if(bottom[s]==)
{
bottom[s]=cnt;
}
Next[cnt]=top[s];
top[s]=cnt;
}
void Pop(int s)
{
if(top[s]==)printf("EMPTY\n");
else
{
printf("%d\n",a[top[s]]);
top[s]=Next[top[s]];
if(top[s]==)bottom[s]=;
}
}
void swapp(int s,int v)
{
if(bottom[s]==)bottom[s]=bottom[v];
Next[bottom[v]]=top[s];
top[s]=top[v];
bottom[v]=top[v]=;
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
memset(bottom,,sizeof(bottom));
memset(top,,sizeof(top));
memset(Next,,sizeof(Next));
int n,Q;scanf("%d%d",&n,&Q);
cnt=;
while(Q--)
{
int op,x,y;scanf("%d",&op);
if(op==)
{
scanf("%d%d",&x,&y);
Push(x,y);
}
else if(op==)
{
scanf("%d",&x);
Pop(x);
}
else
{
scanf("%d%d",&x,&y);
if(bottom[y]==)continue;
swapp(x,y);
}
}
}
return ;
}
ZOJ 4016 Mergeable Stack(栈的数组实现)的更多相关文章
- ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, ther ...
- ZOJ - 4016 Mergeable Stack 【LIST】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...
- ZOJ 4016 Mergeable Stack 链表
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, the ...
- 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 ...
- [ZOJ 4016] Mergable Stack
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 直接用栈爆内存,看网上大神用数组实现的,构思巧妙,学习了! ...
- Mergeable Stack ZOJ - 4016(list)
ZOJ - 4016 vector又T又M list是以链表的方式存储的 是一个双向链表 元素转移操作中,不能一个个遍历加入到s中,list独有的splic函数可以在常数时间内实现合并(并删除源lis ...
- Mergeable Stack(链表实现栈)
C - Mergeable Stack ZOJ - 4016 一开始用stl中内置的栈来写,其中第三个操作,我先复制到一个数组,再将其倒给另一个栈 这个方法有两个错误的地方: 1.栈在内存很大需要扩容 ...
- 基于数组实现Java 自定义Stack栈类及应用
栈是存放对象的一种特殊容器,在插入与删除对象时,这种结构遵循后进先出( Last-in-first-out,LIFO)的原则.java本身是有自带Stack类包,为了达到学习目的已经更好深入了解sta ...
随机推荐
- avaweb学习总结(八)——HttpServletResponse对象(二)
一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,
- 算法总结之 在单链表和双链表中删除倒数第k个节点
分别实现两个函数,一个可以删除单链表中倒数第k个节点,另一个可以删除双链表中倒数第k个节点 思路: 如果链表为空,或者k<1 参数无效 除此之外 让链表从头开始走到尾,每移动一步,就让k的值减1 ...
- Linux 基本命令___0002
来源:https://mp.weixin.qq.com/s/DmfpDfWpWRV3EDItDdYgXQ #配置vim #http://www.cnblogs.com/ma6174/archive/2 ...
- Sqoop将MySQL表结构同步到hive(text、orc)
Sqoop将MySQL表结构同步到hive sqoop create-hive-table --connect jdbc:mysql://localhost:3306/sqooptest --user ...
- java中finally的使用
以前认为finally没用,但是实际上在try使用中是不可缺少的.
- java:输出流程printStream
// TODO 自动生成的方法存根 //路径文件 File file = new File("F:"+File.separator+"work"+File.se ...
- asp.net 锚点
可以使用锚点,但这里可使用灵活处理 首先获取需要滚动到的位置的id,如,可以设置一个元素(,注:要在form里),另外在form的任意位置设置 代码如下: 注:a标签里不要有内容,在回传的地方调用 代 ...
- css3——transition属性和opacity属性
[transition-duration] 是一个css3属性,规定完成过度效果需要花费的时间(一秒或毫秒计).语法:transition-duration: time;time : 规定完成过 ...
- hive从查询中获取数据插入到表或动态分区
Hive的insert语句能够从查询语句中获取数据,并同时将数据Load到目标表中.现在假定有一个已有数据的表staged_employees(雇员信息全量表),所属国家cnty和所属州st是该表的两 ...
- PowerCmd 2.2 注册码
PowerCmd 是共享软件,现分享注册码一枚: 用户名:Sub 注册码:PCMDA-86128-PCMDA-70594 来源:http://blog.csdn.net/subchen/article ...