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 ...
随机推荐
- day01代码
1. 使用while循环打印1,2,3,4,5,7,8,9 # 使用while循环打印1,2,3,4,5,7,8,9 count = 0 while count < 10: count += 1 ...
- 关于mysql 的 autoCommit 参数
首先描述一下这个参数的作用. 这个参数 默认是开启的. 开启以后再命令敲的sql 会自动提交.如果关闭,就必须手动 commit. 查看 这个 自动提交状态. SELECT @@autocommit; ...
- shibie
var mStream: TMemoryStream; vcode: ..] of AnsiChar; buffer: array of AnsiChar; begin mStream := TMem ...
- ningx.conf location
server { listen ; server_name localhost; location /dirName { alias "C:/Users/VALEB/Downloads/in ...
- Windows Server 2008 R2 /2012 修改密码策略
今天建了域环境,在添加新用户的时候,发现用简单的密码时域安全策略提示密码复杂度不够,于是我就想在域安全策略里面把密码复杂度降低一点. 问题: 在“管理工具 >> 本地安全策略 > ...
- VS重新生成项目失败,但是不报错
相信很多同行可能都遇到这种情况: 使用SVN或者其他项目管理工具checkout一个项目后,重新生成项目后,生成失败了,但是没有显示任何错误: 其实,这个大多数是因为类库里面的引用失效导致的,我们可以 ...
- java 类的初始化顺序
有父类 1. 父类static成员变量 2. 父类static块 3. 父类非static成员 4. 父类非static块 5. 父类构造方法 子类,也按照1-5顺序执行 无父类 1. static成 ...
- Difference Between Git and SVN
From: http://www.differencebetween.net/technology/software-technology/difference-between-git-and-svn ...
- 如何在宿主机上执行容器里的jmap,jtack,jstat 命令获取信息(原创)
一般情况下,我们要获取docker容器里的jvm信息只能进入容器后执行jmap,jstack,jstat 命令去获取,jstack,jstat还好,但是jmap dump的文件要拿出来,得先copy ...
- linux中的查找命令find,locate,which,whereis
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料,因为有时很长时间不会用到,当要用的时候经常弄混了. which 查看可执行文 ...