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 ...
随机推荐
- Django开发问题及解决方法汇总
1. manage.py@MxOnline > makemigrations users manage.py@MxOnline > migrate users 2. 操作django的ad ...
- Scrum 团队成立 -- 软件工程
团队项目选题 : 金融工具:复利计算与投资记录项目继续升级,开发定位明确.功能专注的工具类软件 团队队员 : 蔡舜 , 林宇粲 , 王昕明 , 卢晓洵 团队目标 : 不断完善 团队口号 : 永不 ...
- linux永久关闭防火墙
- Java数据结构和算法(二)顺序存储的树结构
Java数据结构和算法(二)顺序存储的树结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 二叉树也可以用数组存储,可以和完 ...
- linux小白
1. linux下加域名. 文件是在/etc/hosts 中间加的tab键 192.168.0.1 baidu.com linux下测试网页可以用 wget www.baidu.com 这个命 ...
- Plugin 'Scala' is incompatible with this.installation
==问题=== 手动安装IDEA的Scala插件,报这个错误. ===原因=== IDEA的版本与Scala插件的版本不兼容. ===解决=== 1.查看一下IDEA的版本 2.下载对应版本的Scal ...
- 2018.10.12 bzoj4712: 洪水(树链剖分)
传送门 树链剖分好题. 考虑分开维护重儿子和轻儿子的信息. 令f[i]f[i]f[i]表示iii为根子树的最优值,h[i]h[i]h[i]表示iii重儿子的最优值,g[i]g[i]g[i]表示iii所 ...
- 2018.09.28 牛客网contest/197/B面积并(二分+简单计算几何)
传送门 比赛的时候把题目看成求面积交了,一直没调出来. 下来发现是面积并气的吐血. 码了一波发现要开long double. 然而直接用现成的三角函数会挂. 因此需要自己手写二分求角度. 大致思路就是 ...
- 2018.08.16 POJ1183反正切函数的应用(简单数学)
传送门 代数变形一波. 显然有b,c>a. 那么这样的话可以令b=a+m,c=a+n. 又有a=(bc-1)/(b+c). 带入展开可知m*n=a*a+1. 要让m+n最小只需让m最大,这个结论 ...
- Shell 中expr的使用
1.expr命令一般用于整数值,其一般格式为:expr argument operator argument一般的用法是使用expr做算术运算,如:[root@centos ~]# expr 10 + ...