ZOJ  4016

list用法https://www.cnblogs.com/LLLAIH/p/10673068.html

一开始用普通的栈做,超内存,链表模拟栈也没写出来orz.
补题发现list超好用,真的-6-

有三个操作:1/向栈里添加数

2/输出栈顶元素然后pop掉

3/将栈b合并到栈a里并将栈b清空(注意合并后的顺序)

注意注意注意!!一定要将需要使用的list元素进行清空否则会WA!!

注意合并两个list序列时,用merge的话,会将合并后的序列进行默认的升序排列,所以这题要用splice

#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <list>
#include <map>
#include<stack>
#include<vector>
#define eps 1e-6
#define mod 1000000000
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define MAX 3e5+5
#define read(x) scanf("%d",&x)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=;
typedef long long LL;
list<int> a[maxn];
int main(){
int t,n,q,s,v;
scanf("%d",&t);
while(t--){
int op;
list<int>::iterator it;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
a[i].clear();//清空list里每个元素
for(int i=;i<=q;i++){
scanf("%d",&op);
if(op == ){
scanf("%d%d",&s,&v);
a[s].push_back(v);//将元素从尾部插入
}
else if(op == ){
scanf("%d",&s);
if(a[s].empty())//判断该栈是否为空
{
printf("EMPTY\n");
continue;
}
printf("%d\n",a[s].back());//输出栈顶元素即尾部第一个元素
a[s].pop_back();//将栈顶元素弹出
}
else {
scanf("%d%d",&s,&v);
a[s].splice(a[s].end(),a[v]);//将栈v合并到栈s,并清空栈v
}
}
}
return ;
}

C Mergeable Stack(list超好用)的更多相关文章

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

  7. ZOJ - 4016 Mergeable Stack 【LIST】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...

  8. zoj4016 Mergeable Stack

    题意:对n个栈,有q次操作.每个操作可能为三种情况中的一种:1.将v插入到s栈的顶端:2.输出s栈的栈顶(若栈为空则输出empty):3.将栈t插入到栈s的栈顶. 开始考虑到指针可能会mle,用数组模 ...

  9. The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack

    题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...

随机推荐

  1. webpack学习笔记(六)优化

    1 loader 使用include,缩小编译范围: rules: [{ test: /\.js$/, include: path.resolve(__dirname, '../src'), use: ...

  2. C++——智能指针unique_ptr的实现

    起初,我最直观的设计想法,直接设计一个类:包含全部要素(对象,指针计数).然后提供出去. class CPoint { public: CPoint(, ) : x(xVal), y(yVal) {} ...

  3. Battle Zone 战争地带

    发售年份 1980 平台 街机 开发商 雅达利(Atari) 类型 射击 https://www.youtube.com/watch?v=Ctr54kopo8I

  4. java8_api_nio

    NIO-1    nio的概念    Buffer的属性    Buffer中数据的读写        用以提高IO处理数据的性能问题,之前io里的单位是Byte(java程序向流中写入byte或相反 ...

  5. chrome添加离线插件

    1.首先用户点击谷歌浏览器右上角的自定义及控制按钮,在下拉框中选择工具选项,然后点击扩展程序来启动Chrome浏览器的扩展管理器页面. 2.在打开的谷歌浏览器的扩展管理器中用户可以看到一些已经安装程序 ...

  6. 一、python中的一切皆对象

    1.函数和类是对象,是Python中的一级公民 (1)将函数和类赋值给一个变量 将函数或类赋值给一个变量,就可以把该变量当成函数或类来使用 def ask(name): print(name) cla ...

  7. 剑指offer 7. 递归和循环 斐波那契数列

    题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 简简单单 废话不多说,直接上代码: public class Sol ...

  8. mysql update where

    UPDATE car_approval a JOIN car_distribute b ON a.id = b.APPROVAL_FOR_CAR_ID SET a.APPROVAL_STATUS = ...

  9. Django-Form组件之字段

    Form类 创建Form类时,主要涉及到 [字段] 和 [插件],字段用于对用户请求数据的验证,插件用于自动生成HTML; 1.Django内置字段如下: 1 2 3 4 5 6 7 8 9 10 1 ...

  10. 批量删除ppt动画

    alt+F11打开宏编辑窗口,输入以下代码,运行即可: Sub removeALL() Dim I As Integer: Dim J As Integer Dim oActivePres As Ob ...