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 题意 t组样例 每组 n个栈 q个操作 操作1 将 val 放到第i个栈顶
操作2 输出 第 i 个栈的栈顶 弹出栈顶 为空 输出EMPTY
操作3 将 第 j个栈 整个栈 放到第i个栈的栈顶 解析 用栈的话会MLE TLE 要用到c++容器 list 操作三就是o(1)复杂度 内存也没有问题 AC代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <utility>
#include <stack>
#include <list>
using namespace std;
const int maxn = 3e5+,inf = 0x3f3f3f3f, mod = ;
const double epx = 1e-;
const double PI = acos(-1.0);
typedef long long ll;
list<int> l[maxn];
int main()
{
int t,n,q;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&q);
for(int i=;i<=n;i++)
l[i].clear();
while(q--)
{
int m,i,j,val;
scanf("%d",&m);
if(m==)
{
scanf("%d%d",&i,&val);
l[i].push_back(val);
}
else if(m==)
{
scanf("%d",&i);
if(l[i].empty())
printf("EMPTY\n");
else
{
printf("%d\n",l[i].back());
l[i].pop_back();
}
}
else
{
scanf("%d%d",&i,&j);
l[i].splice(l[i].end(),l[j]);
}
}
}
}
https://blog.csdn.net/gscsdlz/article/details/52130225
https://blog.csdn.net/gogokongyin/article/details/51178378
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(栈的数组实现)
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 ...
- Mergeable Stack(链表实现栈)
C - Mergeable Stack ZOJ - 4016 一开始用stl中内置的栈来写,其中第三个操作,我先复制到一个数组,再将其倒给另一个栈 这个方法有两个错误的地方: 1.栈在内存很大需要扩容 ...
- [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 ...
- C Mergeable Stack(list超好用)
ZOJ 4016 list用法https://www.cnblogs.com/LLLAIH/p/10673068.html 一开始用普通的栈做,超内存,链表模拟栈也没写出来orz.补题发现list超 ...
随机推荐
- 对于JS == 运算的一些理解
声明:本文是摘自一篇文章,放在这只为做为一个笔记能更好学习. 大家知道,==是JavaScript中比较复杂的一个运算符.它的运算规则奇怪,容易让人犯错,从而成为JavaScript中“最糟糕的特性” ...
- 使用 ServerSocket 进行文件上传,以及用Tomcat启动ServerSocket时,会卡死解决
服务器端代码 import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOExcept ...
- CCF|碰撞的小球
import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner scan ...
- window下phpstudy开启redis扩展
注:一定要注意自己PHP的版本结构是64还是32位的!其次查看PHP Extension Build是NTS or TS! 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本(特 ...
- java 对sql格式化
public class SqlFormat{ public static void main(String[] args){ String sql=""; sqlFormat(s ...
- ArrayList源码分析(基于JDK1.8)
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess ...
- Java常用工具类---IP工具类、File文件工具类
package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...
- CSS3 radio 或checkbox 自定义 样式
.style-radio {position:relative;width:15px;height:15px;outline:none;} .style-radio:after {position:a ...
- idea 中pom.xml依赖版本号报错(报红,如下图所示)
1.maven工程中出现的错误 2.解决办法:file->setting->Maven 如果还没好的话请尝试以下方法:
- cal - 显示一个日历
总览 cal [-mjy ] [月份 [年份 ] ] 描述 Cal 显示一个简单的日历.. 如果没有指定参数, 则显示当前月份. 选项如下所列: -m 显示星期一作为一周的第一天.. (缺省为星期日. ...