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

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

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4.  
  5. const int maxn = 3e5+;
  6. struct node{
  7. long long data;
  8. long long pre;
  9. }arr[maxn];
  10. struct list{
  11. long long top;
  12. long long head;
  13. }lst[maxn];
  14. long long used;
  15.  
  16. void insert(long long s, long long v){
  17. arr[used].data = v;
  18. arr[used].pre = lst[s].top;
  19. lst[s].top = used;
  20. if (lst[s].head == -)
  21. lst[s].head = used;
  22. used++;
  23. }
  24. void pop(long long s){
  25. if (lst[s].top == -)
  26. printf("EMPTY\n");
  27. else{
  28. printf("%d\n", arr[lst[s].top].data);
  29. lst[s].top = arr[lst[s].top].pre;
  30. if (lst[s].top == -)
  31. lst[s].head = -;
  32. }
  33. }
  34. void move(long long s, long long t){ //把t放到s上
  35. if (lst[t].top != -){
  36. arr[lst[t].head].pre = lst[s].top;
  37. if (lst[s].top==-) lst[s].head=lst[t].head;
  38. lst[s].top = lst[t].top;
  39. lst[t].top = lst[t].head = -;
  40. }
  41. }
  42.  
  43. int main(){
  44. int T;
  45. scanf("%d", &T);
  46.  
  47. while (T--){
  48. long long n,q;
  49. scanf("%lld%lld", &n, &q);
  50. used = ;
  51. memset(lst, -, sizeof(lst));
  52. long long s,t,mod;
  53. long long v;
  54. while (q--){
  55. scanf("%lld", &mod);
  56. if (mod == ){
  57. scanf("%lld%lld", &s, &v);
  58. insert(s, v);
  59. }
  60. else if (mod == ){
  61. scanf("%lld", &s);
  62. pop(s);
  63. }
  64. else {
  65. scanf("%lld%lld", &s, &t);
  66. move(s, t);
  67. }
  68. }
  69. }
  70.  
  71. return ;
  72. }

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. Django开发问题及解决方法汇总

    1. manage.py@MxOnline > makemigrations users manage.py@MxOnline > migrate users 2. 操作django的ad ...

  2. Scrum 团队成立 -- 软件工程

      团队项目选题  : 金融工具:复利计算与投资记录项目继续升级,开发定位明确.功能专注的工具类软件 团队队员 : 蔡舜 , 林宇粲 , 王昕明 , 卢晓洵 团队目标 : 不断完善 团队口号 : 永不 ...

  3. linux永久关闭防火墙

  4. Java数据结构和算法(二)顺序存储的树结构

    Java数据结构和算法(二)顺序存储的树结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 二叉树也可以用数组存储,可以和完 ...

  5. linux小白

    1. linux下加域名. 文件是在/etc/hosts    中间加的tab键 192.168.0.1 baidu.com linux下测试网页可以用 wget www.baidu.com  这个命 ...

  6. Plugin 'Scala' is incompatible with this.installation

    ==问题=== 手动安装IDEA的Scala插件,报这个错误. ===原因=== IDEA的版本与Scala插件的版本不兼容. ===解决=== 1.查看一下IDEA的版本 2.下载对应版本的Scal ...

  7. 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所 ...

  8. 2018.09.28 牛客网contest/197/B面积并(二分+简单计算几何)

    传送门 比赛的时候把题目看成求面积交了,一直没调出来. 下来发现是面积并气的吐血. 码了一波发现要开long double. 然而直接用现成的三角函数会挂. 因此需要自己手写二分求角度. 大致思路就是 ...

  9. 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最大,这个结论 ...

  10. Shell 中expr的使用

    1.expr命令一般用于整数值,其一般格式为:expr argument operator argument一般的用法是使用expr做算术运算,如:[root@centos ~]# expr 10 + ...