ZOJ - 4016 Mergeable Stack (STL 双向链表)
【传送门】http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016
【题目大意】初始有n个空栈,现在有如下三种操作:
(1) 1 s v 即 s.push(v)
(2) 2 s 即 s.pop() 输出弹出的元素,如果栈s为空则输出 "EMPTY"
(3) 3 s t 把t栈元素全部移到s栈中,使s的尾部与t的首部相连。
现在有若干上述三种类型的操作,遇到操作2则输出相应内容。
【题解】由于站的数量n和操作次数q的数量级都达到1e5,建这么多栈取模拟上述三种操作是不明智的,特别是对于操作2,还要设置一个辅助栈来把元素按序移动到s栈。其实看到操作2就应该想到这应该是用链表实现,而且是双向链表,时间复杂度O(1)
核心方法调用:
assign() 给list赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list的配置器
insert() 插入一个元素到list中
max_size() 返回list能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list的末尾添加一个元素
push_front() 在list的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list删除元素
remove_if() 按指定条件删除元素
rend() 指向list末尾的逆向迭代器
resize() 改变list的大小
reverse() 把list的元素倒转
size() 返回list中的元素个数
sort() 给list排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素
【代码】
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <list> using namespace std;
#define maxsize 300005
list <int> st[maxsize]; int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
for(int i=;i<maxsize;i++)
{
st[i].clear();
}
int n,q;
scanf("%d%d",&n,&q);
int a,b,c;
while(q--)
{
scanf("%d",&a);
if(a == )
{
scanf("%d%d",&b,&c);
st[b].push_back(c);
}
if(a == )
{
scanf("%d",&b);
if(st[b].empty())
{
printf("EMPTY\n");
}
else
{
printf("%d\n",st[b].back());
st[b].pop_back();
} }
if(a == )
{
scanf("%d%d",&b,&c);
st[b].splice(st[b].end(),st[c]);
} }
}
return ;
}
ZOJ - 4016 Mergeable Stack (STL 双向链表)的更多相关文章
- 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 链表
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(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.栈在内存很大需要扩容 ...
- C Mergeable Stack(list超好用)
ZOJ 4016 list用法https://www.cnblogs.com/LLLAIH/p/10673068.html 一开始用普通的栈做,超内存,链表模拟栈也没写出来orz.补题发现list超 ...
随机推荐
- VIM C语言函数名高亮
VIM默认情况下,函数名是不会高亮的,将下面这段代码添加到/usr/share/vim/vim73/syntax/c.vim文件的末尾即可: "highlight Functions s ...
- layuiadmin更新echarts
发现layuiadmin中echarts版本有些老了,对其中的echarts进行了更新: 查询当前echarts版本: console.log(echarts.version); 1.前往echart ...
- Java 的访问权限
public>protected>默认(包访问权限)>private,因为protected除了可以被同一包访问,还可以被包外的子类所访问
- emoji等表情符号存mysql的方法
项目中需要存储用户信息(用户昵称有表情符号),自然就遇到了emoji等表情符号如何被mysql DB支持的问题 这里引用先行者博文:https://segmentfault.com/a/1190000 ...
- python安装numpy模块
1.打开网址https://pypi.python.org/pypi/numpy,找到安装的python版本对应的numpy版本. 我的python版本是 下载的对应numpy版本是 2.将numpy ...
- PyQt5(2)、垃圾分类小程序(2)——初代窗口程序可执行文件
又是一天时间(又没做大作业).今天的心路历程:(1)前端后端怎么连接?(2)后端数据库插数据(3)完全没用上之前的字典反查法(4)突然发现面向对象编程其实很好用,甚至越用越上瘾(5)QLineEdit ...
- Hive 启动报错 URI
Exception in thread "main"java.lang.RuntimeException: java.lang.IllegalArgumentException:j ...
- MFC中关于子进程创建和关闭操作
创建子进程 PROCESS_INFORMATION ProcessInfo; STARTUPINFO StartupInfo; //This is an [in] parameter ZeroMemo ...
- hdu3594 Cactus
仙人掌入门简单题. 先看一篇文档. #include <iostream> #include <cstring> #include <cstdio> using n ...
- luogu2604 [ZJOI2010]网络扩容
先做一遍普通的dinic 然后再更改源点为超级源,超级源向原源加一条capacity=k && cost=0的边,再加上有费用的边跑最小费用最大流 #include <iostr ...