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> ...
 
随机推荐
- Python setup.py和MANIFEST.in文件
			
Setup.py文件 from setuptools import setup from codecs import open # 第三方依赖包及版本号 requires = ['beautifuls ...
 - window.opener
			
window.opener 实际上就是通过window.open打开的窗体的父窗体. 比如在父窗体parentForm里面 通过 window.open("subForm.html" ...
 - Siteserver平台搭建
			
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 一开始什么也不懂真痛 ...
 - Skia构建系统与编译脚本分析
			
分析下Skia的构建系统,详细编译过程參看Windows下从源代码编译Skia.这里以ninja为例来分析.运行以下三条命令就能够完毕编译: SET "GYP_GENERATORS=ninj ...
 - 如何禁止同IP站点查询和同IP站点查询的原理分析  Robots.txt屏蔽BINGBOT
			
很多站长工具中都有“同IP站点查询”.“IP反查域名”这种服务不少人都不知道是什么原理,其实这些服务几乎都是用BING(以前的LIVE)来实现 的,BING有个特别功能 BING抓取页面时会把站点的I ...
 - mysql中的类型转换和精确位数
			
select round(123.5); 四舍五入 select floor(123.5);取整数部分 select ceil(123.5);四舍五入
 - 仿VS安装界面小球滑动效果
			
在Visual Studio 2010后续版本的安装界面中,可以发现一组小球在滑动表示安装程序正在进行: 于是尝试用CSS实现了一下. 首先需要建立用来表示小球的html结构: <div cla ...
 - Linux U盘只读解决方法
			
Linux Fat的U盘只读,这个问题经常出现,原因大家都说了是U盘的错误,出现这种情况后,一般的解决方案是 mount | grep <U盘的标签> # 找到你的U盘的对应的设备名称,如 ...
 - python学习(九)python中的变量、引用和对象的关系
			
<Think In Java>中说到过"万事万物皆对象",这句话也可以用在Python中. 感觉Python中的变量有点像Javascript中的变量,是弱类型的,但是 ...
 - IPv4地址(二)网络划分
			
在IPv4地址(一)中提到过,IP地址可以分成两部分,前面一部分是网络号,而后面一部分是主机号. 这里网络可以通过主机数量规模不同而分为3类:大型网络.中型网络和小型网络. 不同网络的特点 大型网络— ...