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 ...
随机推荐
- webkit开源项目
WebKitOpen Source Web Browser Engine Blog Downloads Feature Status Reporting Bugs Contribute Getting ...
- 【转】MEF程序设计指南二:Silverlight中使用CompositionInitializer宿主MEF
MEF可以在传统应用程序中使用(包括桌面的Winform.控制台程序和Web的ASP.NET),也可以在RIA的Silverlight中使用.在Silverlight中只是宿主的方式有所不同,实际上在 ...
- OpenSource.iOS.ProtobufWithObjective-C
2. 在iOS(Mac OS X)中使用protobuf 2.0 构建protoc A) 下载最新的protobuf版本 B) 依据README中的步骤依次进行 2.1 添加protobuf到工程中 ...
- 2018.09.29 bzoj3885: Cow Rectangles(悬线法+二分)
传送门 对于第一个问题,直接用悬线法求出最大的子矩阵面积,然后对于每一个能得到最大面积的矩阵,我们用二分法去掉四周的空白部分来更新第二个答案. 代码: #include<bits/stdc++. ...
- 2018.07.04 POJ 1654 Area(简单计算几何)
Area Time Limit: 1000MS Memory Limit: 10000K Description You are going to compute the area of a spec ...
- 2018.08.15 bzoj3747: [POI2015]Kinoman(线段树)
传送门 简单题. 先不管时间复杂度看看怎么做. 对于一段区间[l,r],如果从右端加入一个数a[r+1],对这个区间有什么影响?显然如果区间中已经有了a[r+1]这个数就会产生-a[i+1]的影响,否 ...
- HDU 2393 Higher Math (判断直角三角形)
题意:给定三个边,判断是不是直角三角形. 析:水题,勾股定理... 代码如下: #include <iostream> #include <cstdio> #include & ...
- python corrcoef
该函数得到相关系数矩阵. 例子: vc=[1,2,39,0,8] vb=[1,2,38,0,8] print mean(multiply((vc-mean(vc)),(vb-mean(vb))))/( ...
- MATLAB矩阵的一些用法
1.怎样去提取和修改矩阵中的一个元素. (1)创建一个矩阵 >> A=[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16]A = 1 2 ...
- strcpy,strlen, strcat, strcmp函数,strlen函数和sizeof的区别
//计算字符串实际长度 //strlen()函数:当遇到'\0'时,计算结束,'\0'不计入长度之内,如果你只定义没有给它赋初值,这个结果是不定的,它会从首地址一直找下去,直到遇到'\0 ...