zoj4016 Mergeable Stack
题意:对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的更多相关文章
- Mergeable Stack(链表实现栈)
C - Mergeable Stack ZOJ - 4016 一开始用stl中内置的栈来写,其中第三个操作,我先复制到一个数组,再将其倒给另一个栈 这个方法有两个错误的地方: 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 ...
- Mergeable Stack ZOJ - 4016(list)
ZOJ - 4016 vector又T又M list是以链表的方式存储的 是一个双向链表 元素转移操作中,不能一个个遍历加入到s中,list独有的splic函数可以在常数时间内实现合并(并删除源lis ...
- C Mergeable Stack(list超好用)
ZOJ 4016 list用法https://www.cnblogs.com/LLLAIH/p/10673068.html 一开始用普通的栈做,超内存,链表模拟栈也没写出来orz.补题发现list超 ...
- 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 ...
- ZOJ - 4016 Mergeable Stack 【LIST】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack
题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...
随机推荐
- Alluxio/Tachyon如何发挥lineage的作用?
在Spark的RDD中引入过lineage这一概念.指的是RDD之间的依赖.而Alluxio则使用lineage来表示文件之间的依赖.在代码层面,指的是fileID之间的依赖. 代码中的注释指出: * ...
- makefile all
all:udps udpc udps:udpserv.c gcc -Wall -o udps udpserv.cudpc:udpclient.c gcc -Wall -o udpc udp ...
- vue-route 路由传参的使用
1 router/index.js 中的定义 { path: '/product', component: ProductIndex, meta: { requiredAuth: true, }}, ...
- 【JAVA】通过URLConnection/HttpURLConnection发送HTTP请求的方法(一)
Java原生的API可用于发送HTTP请求 即java.net.URL.java.net.URLConnection,JDK自带的类: 1.通过统一资源定位器(java.net.URL)获取连接器(j ...
- 492. Construct the Rectangle
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 2018.07.08 NOIP模拟 ABCD(背包)
ABCD 题目背景 SOURCE:NOIP2016-AHSDFZ T2 题目描述 有 4 个长度为 N 的数组 a,b,c,d .现在需要你选择 N 个数构成数组e ,数组e 满足 a[i]≤e[i] ...
- [K8S]污点调度
如果不希望某个节点被调度可以使用以下命令进行设置 kubectl taint node master01 node-role.kubernetes.io/master="":No ...
- HDU 2136 Largest prime factor (素数打表。。。)
题意:给你一个数,让你求它的最大因子在素数表的位置. 析:看起来挺简单的题,可是我却WA了一晚上,后来终于明白了,这个第一层循环不是到平方根, 这个题和判断素数不一样,只要明白了这一点,就很简单了. ...
- qmake-how to
简单例子 假设已经实现如下程序: hello.cpphello.hmain.cpp 首先,使用编辑器,在上述文件目录下创建文件hello.pro.然后加入几行语句告诉qmake项目中的源文件和头文件. ...
- 8b10b
目的:保持直流平衡DC Balance). running disparity() 8bit原始数据会分成两部分,其低5位会进行5B/6B编码,高3位则进行3B/4B编码,这两种映射关系在当时已经成为 ...