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 ...
随机推荐
- servlet原理分析
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- codeforces 435B
题意:只能对相邻的两个数字进行交换,允许k次交换,输出交换能得到的最大的数.从最高位开始寻找最优,每次寻找能交换的步数里交换到的最大值进行交换. #include<cstdio> #inc ...
- Python httpServer服务器(初级)
使用原生的python开发的web服务器,入门级! #!/usr/bin/python # -*- coding: UTF-8 -*- import os #Python的标准库中的os模块包含普遍的 ...
- ggplot笔记002——qplot()函数
qplot()函数 一年前就听说过ggplot,很多人都说ggplot强大,ggplot无所不能,从今天开始就让我们一起来见证一下这个神奇的R包. 首先要加载ggplot2: 1 if(!suppre ...
- socket io 记得flush
public class Client { public static void main(String args[]) throws Exception { //为了简单起见,所有的异常都直接往外抛 ...
- bash: .bashrc: command not found
解决这个错误需要: vi ~/.bashrc 进入以后把 .bashrc 给注释掉 就不会再报错了.
- vijos 1250 最勇敢的机器人 分组背包+并查集
P1250最勇敢的机器人 背景 Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ 描述 机器人们都想知道谁是最勇敢的,于是它们比赛搬运一些物品. 它们到了一个仓库,里面有n ...
- 执行安装redis报错undefined reference to `__sync_add_and_fetch_4'
执行make命令时报错: zmalloc.o: In function `zmalloc_used_memory': /var/lib/tcommsvr/redis-2.8.0-rc4/src/z ...
- 简述redux(1)
简述redux(1) 概念: 是一个有用的架构,应用场景一般为:多交互.多数据源.如: 某个组件的状态需要共享 某个状态需要在任何地方可以看到 一个组件需要改变全局状态. 一个组件需要改变另一个组件的 ...
- 验证实现element-ui树形控件的自定义图标及右键菜单
许久不用,element-ui已经更新至2.4.1版本.直接进入今天的正题,前提是node.js的环境还有vue及elment-ui都已经安装.由于element-ui的官方文档中介绍比较粗略,试了许 ...