2016 多校联赛7 Joint Stacks (优先队列)
A mergeable stack is a stack with "merge" operation. There are three kinds of operation as follows:
- push A x: insert x into stack A
- pop A: remove the top element of stack A
- merge A B: merge stack A and B
After an operation "merge A B", stack A will obtain all elements that A and B contained before, and B will become empty. The elements in the new stack are rearranged according to the time when they were pushed, just like repeating their "push" operations in one stack. See the sample input/output for further explanation.
Given two mergeable stacks A and B, implement operations mentioned above.
InputThere are multiple test cases. For each case, the first line contains an integer N(0<N≤105)N(0<N≤105), indicating the number of operations. The next N lines, each contain an instruction "push", "pop" or "merge". The elements of stacks are 32-bit integers. Both A and B are empty initially, and it is guaranteed that "pop" operation would not be performed to an empty stack. N = 0 indicates the end of input.OutputFor each case, print a line "Case #t:", where t is the case number (starting from 1). For each "pop" operation, output the element that is popped, in a single line.Sample Input
4
push A 1
push A 2
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge A B
pop A
pop A
pop A
9
push A 0
push A 1
push B 3
pop A
push A 2
merge B A
pop B
pop B
pop B
0
Sample Output
Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0 启发博客:http://www.cnblogs.com/Sunshine-tcf/p/5753964.html
用三个优先队列来维护,最神奇的是,由于题目说POP操作不会对空栈进行,甚至不需要对C这个公共栈标记是A或是B。
详见下:
题意:
给出两个栈A B(初始时为空),有三种操作:
push、pop、merge.
其中merge是按照A B中元素进栈的相对顺序来重排的.
题解:
给每次push的数加上一个时间戳.
维护三个优先队列,按照节点的进栈时间从后往前排序(时间戳从大往小). 这样就保证了列首元素一定是最晚进栈的.
三个优先队列:A和B为题中的栈,COM为公共栈. (即存放合并后的结果,并标记COM当前是A还是B)
①对于push操作,按照正常逻辑入栈(入队).
②对于pop操作,先从A/B中去找,如果A/B为空,则说明存放在COM公共栈中了.
(题目保证了不会对空栈pop,这样一来都不需要再额外标记公共栈COM当前是A还是B了).
③对于merge操作,把当前A和B全部清空并放到公共栈COM中即可.
可以证明,每个元素被移动到COM中的次数不超过1. 所以总体时间复杂度还是O(n).
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std; struct node
{
int num,t;//t表示入栈时间
node(int a,int b){
num=a;t=b;
}//构造函数用于在队列中插入结构体
}; struct cmp//queue的比较函数
{
bool operator()(node a,node b)
{
return a.t<b.t;//top是入栈最晚的,后进先出
}
}; int main()
{
int n,c,i,T=;
char a[],b;
while(~scanf("%d",&n)&&n)
{
printf("Case #%d:\n",T++);
priority_queue<node,vector<node>,cmp>A,B,C;
for(i=;i<n;i++)
{
cin>>a;
if(a[]=='u')//push操作
{
cin>>b>>c;
if(b=='A')
A.push(node(c,i));
else
B.push(node(c,i));
}
else if(a[]=='o')//pop操作
{
cin>>b;
if(b=='A'&&!A.empty())
{
printf("%d\n",A.top().num);
A.pop();
}
else if(b=='B'&&!B.empty())
{
printf("%d\n",B.top().num);
B.pop();
}
else
{
printf("%d\n",C.top().num);
C.pop();
} }
else//merge操作
{
cin>>b>>b;
while(!A.empty())
{
C.push(A.top());
A.pop();
}
while(!B.empty())
{
C.push(B.top());
B.pop();
}
}
}
}
return ;
}
2016 多校联赛7 Joint Stacks (优先队列)的更多相关文章
- 多校7 HDU5818 Joint Stacks
多校7 HDU5818 Joint Stacks 题意:n次操作.模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列 思路:开三个栈,并且用到了merge函数 O(n)的复杂度 #include ...
- hdu 5818 Joint Stacks (优先队列)
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu5737(2016多校联赛第2场D)
题意:给2组数据a和b数组,每次有2种操作:(+,l,r,x)把a数组第l个到第r个元素全置为x,(?,l,r)查询[l,r]之间哪些位置满足a[i]>=b[i](i>=l &&a ...
- 2015 多校联赛 ——HDU5360(贪心+优先队列)
Sample Input 4 8 4 1 3 2 2 1 0 3 5 3 6 4 2 1 7 6 8 3 3 2 0 5 0 3 6 4 5 2 7 7 6 7 6 8 2 2 3 3 3 0 0 2 ...
- 2016 多校联赛7 Elegant Construction
Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, ...
- 2016 多校联赛7 Balls and Boxes(概率期望)
Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...
- HDU 5818 Joint Stacks (优先队列)
Joint Stacks 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5818 Description A stack is a data stru ...
- HDU 5818 Joint Stacks(联合栈)
HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 5818 Joint Stacks
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
随机推荐
- java判断时间为上午,中午,下午,晚上,凌晨
public static void main(String[] args) { Date date = new Date(); SimpleDateFormat df = new SimpleDat ...
- python零碎知识点
0.规范化 使用Ctrl+Alt+L可以将代码排列格式更加规范化 1.浮点数 1.23x109就是1.23e9或者12.3e8:0.000012可以写成1.2e-5 2.字符串 >>> ...
- sessionStorage在项目中的应用
1. 本地存储 Cookie(局限性):用户可以禁用cookie,最多只能存储4kb,cookie有过期时间的(一般我们设置的时间最长1个月,用户使用杀毒软件也可以清除我们的cookie)LocalS ...
- ActiveMQ producer不断发送消息,会导致broker内存耗尽吗?
http://activemq.apache.org/my-producer-blocks.html 回答了这个问题: ActiveMQ 5.x 支持Message Cursors,它默认把消息从内存 ...
- 从tcp的角度看,打开一个网页到底发生了什么
使用wireshark进行抓包分析:新建表达式过滤器,选择协议,字段,匹配方式,应用就能筛选出想要的数据包. 一个示例:(tcp.srcport == 1523 or tcp.dstport == 1 ...
- [转]一次CMS GC问题排查过程(理解原理+读懂GC日志)
这个是之前处理过的一个线上问题,处理过程断断续续,经历了两周多的时间,中间各种尝试,总结如下.这篇文章分三部分: 1.问题的场景和处理过程:2.GC的一些理论东西:3.看懂GC的日志 先说一下问题吧 ...
- python-列表,元组,range
# 列表# lst = ["光头强", 1, True, {}, (1, ), {123}, ["周杰伦",[], "周杰", " ...
- 女生可不可以进入IT行业做Linux运维工程师?
不知从何时起有那么一个不成文的理论:女生不适合做IT.在很多人看来,IT is a men’s world,女生学IT是件匪夷所思的事情.在传统的思维当中,女生只适合从事像教师.会计.公务员等稳定的职 ...
- Unity中Text中首行缩进两个字符和换行的代码
1.首行缩进两个字符 txt.text=“\u3000\u3000” + str: 2.首行缩进两个字符 将输入法换成全角的,在Text属性面板中添加空格即可. 3.换行 “\n” 补充 Uni ...
- 用老毛桃U盘安装:[3]Ghost版Win7系统
用老毛桃自动安装Ghost版Win7的步骤: 1,到网上先下载Ghost版Win7映像文件到硬盘,我放到的是U盘,盘符为Z,如果你愿意,可直接放到硬盘即可,放到硬盘安装速度会快一点. 2,把制作好的老 ...