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超 ...
随机推荐
- C# 使用Epplus导出Excel [3]:合并列连续相同数据
C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...
- bash编程的信号捕获:
bash编程的信号捕获: kill -l KILL无法捕捉: trap 'COMMAND' SIGNAL, 信号捕捉用于:在中途中止时做一些清理操作. 一. trap捕捉到信号之后,可以 ...
- CONTEST1001 题解
PROBLEM A 分析 这个题属于非常基础的输出问题,一般来说见到这种题可以直接复制粘贴即可. 讲解 没有什么详细说明的直接复制粘贴即可.这样不容易出错. 代码 #include <stdio ...
- ubuntu命令行卸载并清理软件
卸载软件,可以使用下面这两种方式之一: sudo apt-get remove --purge [software name] sudo apt-get autoremove --purge [sof ...
- sed快速学习
- docker 安装 openresty
文章来源: 1.拉取镜像 # docker pull openresty/openresty 2.启动openresty # docker run -it --name openresty -p : ...
- 1、初学探讨PYTHON的itchat和wxpy两库
最近好奇学习了python,觉得简单明了,但是最头疼的就是调整空格和调试吧,的确调试不如C#使用visual studio 方便,都是使用print()来调试.也许因为我是菜鸟,如果大家还有更好的方法 ...
- LeetCode(150) Evaluate Reverse Polish Notation
题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...
- 数据库---大数据+hadoop
大数据:hadoop:大数据和hadoop的关系
- django的rest framework框架——认证、权限、节流控制
一.登录认证示例 模拟用户登录,获取token,当用户访问订单或用户中心时,判断用户携带正确的token,则允许查看订单和用户信息,否则抛出异常: from django.conf.urls impo ...