题目链接

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】的更多相关文章

  1. ZOJ - 4016 Mergeable Stack (STL 双向链表)

    [传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) ...

  2. ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given initially empty stacks, ther ...

  3. ZOJ 4016 Mergeable Stack(栈的数组实现)

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given  initially empty stacks, the ...

  4. ZOJ 4016 Mergeable Stack 链表

    Mergeable Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB Given  initially empty stacks, the ...

  5. 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 ...

  6. [ZOJ 4016] Mergable Stack

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 直接用栈爆内存,看网上大神用数组实现的,构思巧妙,学习了! ...

  7. 12. Min Stack【medium】

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  8. 495. Implement Stack【easy】

    Implement a stack. You can use any data structure inside a stack except stack itself to implement it ...

  9. ZOJ 3959 Problem Preparation 【水】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 AC代码 #include <cstdio> ...

随机推荐

  1. many-to-one多对一属性说明

    通过many-to-one元素,可以定义一种常见的与另一个持久化类的关联. 这种关系模型是多对一关联(实际上是一个对象引用-译注):这个表的一个外键引用目标表的 主键字段. <many-to-o ...

  2. Android 使用SharedPreferences数据存储

    自己写了个SP辅助类 尽管写的有点啰嗦,也是自己的成果.例如以下: package com.yqy.yqy_testsputil; import android.annotation.Suppress ...

  3. mongodb springdata 问题处理

    发现spring4-mongo3.2.1 加上用户名密码认证后无法认证的问题. 1.必须在当前使用的数据库下建用户,权限可以给readWrite 2.由于mongodb2和mongodb3的用户认证方 ...

  4. RobotFramework-Selenium2Library--关键字

    Selenium2Library用户关键字 *** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arg ...

  5. json解析:[1]gson解析json

    客户端与服务器进行数据交互时,常常需要将数据在服务器端将数据转化成字符串并在客户端对json数据进行解析生成对象.但是用jsonObject和jsonArray解析相对麻烦.利用Gson和阿里的fas ...

  6. c#线程顺序执行

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. js获取和设置属性的方法

    function square(num){    var total = num*num;//局部变量    return total;}var total = 50;//全局变量var number ...

  8. Linux下实现RAID

    一.实验目的 1.掌握Linux系统下软RAID的实现方法: 2.掌握RAID5的配置过程: 3. 通过实验熟悉RAID.5的特点. 二.实验内容及步骤 1.在VMware中创建一台Linux. 2. ...

  9. 05 redis中的Setbit位图法统计活跃用户

    一:场景=>>>长轮询Ajax,在线聊天时,能够用到 Setbit 的实际应用 场景: 1亿个用户, 每个用户 登陆/做任意操作 ,记为 今天活跃,否则记为不活跃 每周评出: 有奖活 ...

  10. HDU 5338(ZZX and Permutations-用线段树贪心)

    ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...