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超 ...
随机推荐
- orcal中创建和删除表空间和用户
1.创建表空间 create tablespace NW_DATA logging datafile 'F:\oracle\product\10.2.0\oradata\nwdb\NW_DATA.db ...
- A*和IDA*介绍
\(A*\)算法是一种很神奇的搜索方法,它属于启发式搜索中的一种.A*最主要的功能当然就是用来剪枝,提高搜索的效率.A*主要的实现方法是通过一个估价函数,每次对下一步进行一个估价,根据估价出的值来决定 ...
- getBean(class )并发下性能较差,有锁.
spring 版本3.1.2 1. spring 并没有缓存 class -> beanDifinition 或者 sington 实例的缓存. 2. 只能先获取所有的beanDifitions ...
- C# Excel常用控件总结
参考:https://blog.csdn.net/waterstar50/article/details/80590355 1.ClosedXML2.EPPlus 教程:http://www.cnbl ...
- java面试宝典第二弹
arraylist和linklist的区别,hashmap和hashset的区别,常用的集合有哪些 一.基础内容 容器就是一种装其他各种对象的器皿.java.util包 容器:Set, List, M ...
- mycat中间件安装与使用
前提: 安装JDK版本在7.0及其以上 1.下载: 下载地址在:http://dl.mycat.io/ 选择1.6-release版本下载 2.安装: 直接解压即可: tar -zxf Mycat-s ...
- perl学习之:read
perl read()函数例子,read()函数实例代码 - 读取,或试图读取,长度LENGTH 与文件句柄FILEHANDLE 相关联的文件到缓冲区BUFFER中的字节数. 语法 read FILE ...
- Could not connect to Redis at IP No route to host
这个问题是在用远程去访问redis出现的 原因:是服务器新装系统 iptables这个的问题 解决办法: sudo iptables -F 轻松解决
- 【php】 get 和 post 比较
来源 php.net 评论区 -- nucc1 worth clarifying: POST is not more secure than GET. The reasons for choosing ...
- Linux配置使用SSH Key登录并禁用root密码登录(替换同理)
Linux系统大多说都支持OpenSSH,生成公钥.私钥的最好用ssh-keygen命令,如果用putty自带的PUTTYGEN.EXE生成会不兼容OpenSSH,从而会导致登录时出现server r ...