题意:对n个栈,有q次操作。每个操作可能为三种情况中的一种:1.将v插入到s栈的顶端;2.输出s栈的栈顶(若栈为空则输出empty);3.将栈t插入到栈s的栈顶。

开始考虑到指针可能会mle,用数组模拟链表来实现。迷之wa,中间少写一句,若s栈为空,则s栈的栈顶变为t栈的栈顶。

 #include <iostream>
#include <cstring>
#include <cstdio> const int maxn = 3e5+;
struct node{
long long data;
long long pre;
}arr[maxn];
struct list{
long long top;
long long head;
}lst[maxn];
long long used; void insert(long long s, long long v){
arr[used].data = v;
arr[used].pre = lst[s].top;
lst[s].top = used;
if (lst[s].head == -)
lst[s].head = used;
used++;
}
void pop(long long s){
if (lst[s].top == -)
printf("EMPTY\n");
else{
printf("%d\n", arr[lst[s].top].data);
lst[s].top = arr[lst[s].top].pre;
if (lst[s].top == -)
lst[s].head = -;
}
}
void move(long long s, long long t){ //把t放到s上
if (lst[t].top != -){
arr[lst[t].head].pre = lst[s].top;
if (lst[s].top==-) lst[s].head=lst[t].head;
lst[s].top = lst[t].top;
lst[t].top = lst[t].head = -;
}
} int main(){
int T;
scanf("%d", &T); while (T--){
long long n,q;
scanf("%lld%lld", &n, &q);
used = ;
memset(lst, -, sizeof(lst));
long long s,t,mod;
long long v;
while (q--){
scanf("%lld", &mod);
if (mod == ){
scanf("%lld%lld", &s, &v);
insert(s, v);
}
else if (mod == ){
scanf("%lld", &s);
pop(s);
}
else {
scanf("%lld%lld", &s, &t);
move(s, t);
}
}
} return ;
}

zoj4016 Mergeable Stack的更多相关文章

  1. Mergeable Stack(链表实现栈)

    C - Mergeable Stack ZOJ - 4016 一开始用stl中内置的栈来写,其中第三个操作,我先复制到一个数组,再将其倒给另一个栈 这个方法有两个错误的地方: 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. Mergeable Stack ZOJ - 4016(list)

    ZOJ - 4016 vector又T又M list是以链表的方式存储的 是一个双向链表 元素转移操作中,不能一个个遍历加入到s中,list独有的splic函数可以在常数时间内实现合并(并删除源lis ...

  6. C Mergeable Stack(list超好用)

    ZOJ  4016 list用法https://www.cnblogs.com/LLLAIH/p/10673068.html 一开始用普通的栈做,超内存,链表模拟栈也没写出来orz.补题发现list超 ...

  7. Mergeable Stack 直接list内置函数。(152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    题意:模拟栈,正常pop,push,多一个merge A B 形象地说就是就是将栈B堆到栈A上. 题解:直接用list 的pop_back,push_back,splice 模拟, 坑:用splice ...

  8. ZOJ - 4016 Mergeable Stack 【LIST】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...

  9. The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack

    题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...

随机推荐

  1. MEME(Motif-based sequence analysis tools)使用说明

    MEME(Motif-based sequence analysis tools)使用说明 2011-05-27 ~ ADMIN MEME是用于从一堆序列中搜索功能结构域的工具.比如说当你拿到了许多C ...

  2. apt-get clean 清除 apt 的缓存

    #apt-get clean 再 df -h 看看,是不是可用空间增加了几个 G ?

  3. 5. Import the project download from Git

    1.Recover eclipse project Copy .project from other project(You can new a project first in eclipse. T ...

  4. android通过 Intent 传递类对象

    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...

  5. 2018.09.28 hdu5434 Peace small elephant(状压dp+矩阵快速幂)

    传送门 看到n的范围的时候吓了一跳,然后发现可以矩阵快速幂优化. 我们用类似于状压dp的方法构造(1(1(1<<m)∗(1m)*(1m)∗(1<<m)m)m)大小的矩阵. 然后 ...

  6. time & datetime 模块

    在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime,calendar(很少用,不讲),下面分别来介绍. 在开始之前,首先要说明几点: 一 ...

  7. 城市边界线预测(根据灯光指数)(PUL)

    1.EXEALL.m function EXEALL(FilePath, FileName)%执行所有流程% FilePath: 文件夹所在路径% FileName: 文件夹名称 FullPath = ...

  8. ZOJ2482 IP Address 2017-04-18 23:11 44人阅读 评论(0) 收藏

    IP Address Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose you are reading byte streams fr ...

  9. B样条参数曲线学习(1)

    B样条参数曲线学习 Bezier曲线有许多优越性,但有两点不足: (1) 特征多边形的顶点个数决定了Bezier曲线的阶次,并且在阶次较大时,特征多边形对曲线的控制将会减弱: (2) Bezier曲线 ...

  10. shell 脚本 抽取指定数量的随机学生

    #!/bin/bash # #!/bin/bash # read -p '输入数' c #指定抽取的学生人数 jw=('王浩' '谢云生' '黄科杨' '何星宇' '张宸兵' '邓培林' '刘桃' ' ...