C Mergeable Stack(list超好用)
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超好用)的更多相关文章
- 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 ...
- 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 将指定元素压入指 ...
- zoj4016 Mergeable Stack
题意:对n个栈,有q次操作.每个操作可能为三种情况中的一种:1.将v插入到s栈的顶端:2.输出s栈的栈顶(若栈为空则输出empty):3.将栈t插入到栈s的栈顶. 开始考虑到指针可能会mle,用数组模 ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack
题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...
随机推荐
- Hadoop MapReduce2.0(Yarn)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/cqboy1991/article/details/25056283 MapReduce2.0(Yar ...
- 事件冒泡(event bubbling)与事件捕捉(event capturing)
事件捕捉: 单击<div>元素就会以下列顺序触发click 事件. Document => Element html => Element body => Element ...
- k8s基于CA签名的双向数字证书认证(三)
1.设置kube-apiserver的CA证书相关的文件和启动参数 1)创建CA证书和私钥相关的文件 openssl genrsa -out ca.key openssl req -x509 -n ...
- mysql主从原理及配置
一.mysql集群架构: 1.一主一从 2.双主 3.一主多从(扩展mysql的读性能) 4.多主一从(5.7开始支持) 5.联机复制 关系图: 二.配置主从用途及条件 2.1用途 1.保障可用性,故 ...
- 微软MSDN原版Windows Server 2008 R2 With SP1下载
Windows Server 2008 R2是windows 服务器版本Windows Server 2008 R2继续提升了虚拟化.系统管理弹性.网络存取方式,以及信息安全等领域的应用,其中有不少功 ...
- ionic3使用echarts
1.安装typings及echarts npm install typings echarts --global 2.安装 ECharts 的 TypeScript 定义文件 npm install ...
- IDEA编译时出现 Information:java: javacTask: 源发行版 1.8 需要目标发行版 1.8
错误如下 检查下面几个地方设置是否一致... 1.Project Structure下(File->Project Structure...) Sources选项卡->Languate l ...
- 安装git-review
参考 https://blog.csdn.net/qq18340811755/article/details/80965188 当yum install git-review安装失效,没有安装包时,只 ...
- python3学习笔记十(循环语句)
参考http://www.runoob.com/python3/python3-loop.html 循环语句 while循环 # !/usr/bin/env python3 n = 100 sum = ...
- 知识点总结——STL相关(持续补充)
---恢复内容开始--- C++ STL 与ACM竞赛相关的应用 1.vector vector是动态数组,可以理解为是能够根据需要随时申请内存的动态数组. 常用操作如下: 容量 vec.size() ...