题意:对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. 操作系统——MiniDos

    #include <stdio.h> #include <string.h> #include <windows.h> ],token[],ch,sa[]; ]={ ...

  2. 02 Maven 入门使用

    Maven 入门使用 1. Maven 项目工程目录约定 Project |-src | |-main | | |-java -- 存放项目的 .java 文件 | | |-resources -- ...

  3. Linux Vim替换字符串的一些方法小结

    使用Linux环境 进行开发工作的程序猿经常有编辑器之争,是vim牛还是emacs棒.二者都是程序猿的开发神器,不管用好哪一个都会使你的工作事半功倍. 本文重点介绍 Vim的替换字符串方法技巧,这些方 ...

  4. 基于SceneControl单击查询功能的实现

    private void HandleIdentify_MouseDown(object sender, ISceneControlEvents_OnMouseDownEvent e) { this. ...

  5. Hi,everybod,搬家了

    Hi,小伙伴们,我的博客已经搬家到了GitHub,地址是:http://wxb.github.io/Blog/ 博客园以后就不维护了,有什么问题咱们GitHub上讨论啦,可以在:https://git ...

  6. Flex box 弹性盒子布局

    display: -webkit-flex 标识使用弹性布局 flex: num  占容器的比例   Flex等比划分 导航1 : Flex :1;  导航2  Flex: 2;     ————&g ...

  7. (转) MVC身份验证及权限管理-2

    转自:http://www.cnblogs.com/ldp615/archive/2010/10/27/asp-net-mvc-forms-authentication-roles-authoriza ...

  8. (转)code first基础

    转自:http://tech.it168.com/a2011/0719/1220/000001220362_all.shtml [IT168 技术]随着.NET 4.0时代的到来,开发者越来越关注如何 ...

  9. QGIS Server Quickstart

    http://live.osgeo.org/en/quickstart/qgis_mapserver_quickstart.html

  10. hdu 5024 最长的L型

    http://acm.hdu.edu.cn/showproblem.php?pid=5024 找到一个最长的L型,L可以是斜着的 简单的模拟 #include <cstdio> #incl ...