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. python MySQL执行SQL查询结果返回字典

    写自动化测试的时候我希望执行数据库前置任务,把数据库查询的结果作为请求的参数,但是正常返回结果为列表嵌套里面,这样就会影响到关键字准确的获取,特别的受限于SQL的查询字段的的顺序,所以希望返回的单条数 ...

  2. repo/repo init-解决同步源码Cannot get http://gerrit.googlesource.com/git-repo/clone.bundle

    以下转自:http://www.cnblogs.com/dinphy/p/5669384.html 问题: fatal: Cannot get https://gerrit.googlesource. ...

  3. 关于使用colorbox加载html页面的一些问题

    ColorBox是一个基于jQuery 1.3 的轻量级,自定义灯箱插件,功能非常强大,支持图片,图片组,ajax,inline和iframed内容,灯箱样式完全由用户控制,可自定义CSS样 式,不需 ...

  4. 1、编写一个简单Makefile模板

    一.Makefile简介 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译, ...

  5. android okHttp 无线程Post请求

    1.导入okhttp-2.7.5.jar和okio-1.11.0.jar包 2.Post无线程请求 public void getDataPost(){ String s = "{\&quo ...

  6. C# Json 和对象的相互转换

    Json 和对象的相互转换 /// <summary> /// Json 和对象的相互转换 ——需引用using Newtonsoft.Json; 即:Jsonnet.net..... / ...

  7. Java Socket NIO

    服务端: public class NIOServer { private static final String HOST = "localhost"; private stat ...

  8. 京东饭粒捡漏V1.15

    20181105 更新 V1.151.部分BUG修复: 功能介绍1.京东商城专用,支持饭粒模式下单,自己获得京豆返利 2.捡漏模式:帮助用户监控抢购商品,有库存的时候进行抢单,主要是通过添加商品ID ...

  9. 和2018年年初做管理系统的不同(vuex)

    从2017年底开始做公司批改后台系统(服务内部人员对熊猫小课用户的作业进行批改.对批改员工资结算等)到教务系统(服务于内部人员对熊猫小课等移动端产品的内容进行配置等).ai-boss系统(服务于内部人 ...

  10. maven项目pom.xml添加main启动类

    pom.xml配置添加main启动类: <build> <finalName>MyApp</finalName> <!-- 最终package打包的jar名称 ...