ZOJ - 4016 Mergeable Stack 【LIST】
题目链接
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016
题意
模拟栈的三种操作
第一种 push 将指定元素压入指定栈
第二种 pop pop出指定栈的栈顶元素 如果栈空 输出 EMPTY
第三种 Move a b 将 b 中的所有元素 移动到 栈a中
思路
本来想到用 双端队列 因为 在移动的时候 比较方便 但是MLE了
后来想到用链表 但是在 比赛的时候 没有想到 有 STL 中 有LIST 这个容器 手写链表 然后 WA了
后来回来后 查了查 LIST 容器 用了一个 SPLICE 就可以模拟第三种操作了
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 3e5 + 5;
const int MOD = 1e9 + 7;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, q;
scanf("%d%d", &n, &q);
list <int> l[maxn];
for (int i = 0; i < q; i++)
{
int op;
int a, b;
scanf("%d", &op);
if (op == 1)
{
scanf("%d%d", &a, &b);
l[a].push_front(b);
}
else if (op == 2)
{
scanf("%d", &a);
if (l[a].size() == 0)
printf("EMPTY\n");
else
{
printf("%d\n", l[a].front());
l[a].pop_front();
}
}
else if (op == 3)
{
scanf("%d%d", &a, &b);
l[a].splice(l[a].begin(), l[b]);
}
}
}
}
ZOJ - 4016 Mergeable Stack 【LIST】的更多相关文章
- ZOJ - 4016 Mergeable Stack (STL 双向链表)
[传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) ...
- 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(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 直接用栈爆内存,看网上大神用数组实现的,构思巧妙,学习了! ...
- 12. Min Stack【medium】
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- 495. Implement Stack【easy】
Implement a stack. You can use any data structure inside a stack except stack itself to implement it ...
- ZOJ 3959 Problem Preparation 【水】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 AC代码 #include <cstdio> ...
随机推荐
- 安全 -- mysql参数化查询,防止Mysql注入
参数化查询(Parameterized Query 或 Parameterized Statement)是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数(Parameter) ...
- htop简介
htop可谓top的升级版,top不可以鼠标操作,但是htop可以使用鼠标操作 启动方式:命令行下输入htop直接启动,启动后的界面如图所示 操作:可以使用上下左右方向键进行移动查看.可以使用鼠标点击 ...
- ylb:SQL 索引(Index)
ylbtech-SQL Server: SQL Server-SQL 索引(Index) SQL 索引(Index). ylb:索引(Index) 返回顶部 --=================== ...
- Linux执行命令unable to create new native thread问题
对于系统的Linux的问题 主要是线程数有限制max user processes 参数限制 修改这个参数涉及到修改两个文件 vi /etc/security/limits.conf 增加如下内容: ...
- 2016.10.18kubernetes 的8080和6443端口的区别与联系
由看过的资料知道,可以使用kubectl,client libraries和REST请求来访问api. 来自官方资料: By default the Kubernetes APIserver se ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- Java内存泄漏及分析
对于内存泄漏,首先想到的是C语言,其实不然,java中也有各种的内存泄漏.对于java程序员,在虚拟即中,不需要为每一个新建对象去delete/free内存,不容易出现内存泄漏.但是,正 是由于这种机 ...
- 应用程序之TableView简单总结1
UITableView的代理方法 修改Cell的状态 UITableView常见方法 一.UITableView的代理方法 #pragma mark 每一行的高度 - (CGFloat)tableVi ...
- 世纪怎么换算成具体的年份?eg:19世纪60年代=19??年?
http://zhidao.baidu.com/question/339742625.html&__bd_tkn__=6ab5183c226b84031b08b849ecac35b396039 ...
- (八)jQuery中的事件
1.加载DOM在常规的JavaScript中,使用window.onload方法:而在jQuery中,使用$(document).ready(function(){ })方法.window.onloa ...