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超 ...
随机推荐
- AJPFX关于static总结
static 总结 static Fields static Methods static member class static initializer-- ...
- ViewPager讲解以及ViewFlipper
1.加入ViewPager最好导入<android.support.v4.view.ViewPager>兼容低版本 2.将布局转换为View的方法 3.适配器类型 PagerAdapter ...
- select在数据库中有两种含义
select在数据库中有两种意思 (1)是赋值的意思(2)是输出,打印的意思我想你问的大概是赋值吧print和 select在数据库中都有打印输出的意思 用法是:select @aa=select* ...
- Log4net快速搭建
nuget安装log4net 2018.12.10当前版本为2.0.8 找到所在项目的[Properties->AssemblyInfo] 在底部加上 [assembly: log4net.Co ...
- PHP流程控制考察点
php遍历数组的三种方法及各自的区别 php遍历数组的方式主要有三种: for循环 foreach循环 while.list().each()组合循环 其中: for循环只能遍历索引数组,foreac ...
- 反射(hasattr,getattr,delattr,setattr)
反射(hasattr,getattr,setattr,delattr) 反射在类中的使用 反射就是通过字符串来操作类或者对象的属性 反射本质就是在使用内置函数,其中反射有四个内置函数: hasattr ...
- 详解 pcap_findalldevs_ex
pcap是packet capture的缩写.意为抓包. 功能:查找所有网络设备 原型:int pcap_findalldevs_ex(char* source, struct pcap_rmtau ...
- Android图像处理之BitMap(2)
Bitmap 相关 1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况: * png图片 如:R.drawable.tianjin J ...
- vue slot的使用介绍
插槽:slot (不知道我这样理解是不是对的,欢迎大佬指点) 具体是什么样子的,请看例子说明 父组件代码 子组件代码 结果 可以看到 ,结果是父组件里面内容显示了,子组件内容显示了,但是在父组件中插入 ...
- hdu 1792 A New Change Problem(互质数之间最大不能组合数和不能组合数的个数)
题意:求互质的m和n的最大不能组合数和不能组合数的个数 思路:m和n的最大不能组合数为m*n-m-n,不能组合数的个数为(m-1)*(n-1)/2 推导: 先讨论最大不能组合数 因为gcd(m,n)= ...