A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another word, the operations perform in a Last-In-First-Out (LIFO) manner. 
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 (优先队列)的更多相关文章

  1. 多校7 HDU5818 Joint Stacks

    多校7 HDU5818 Joint Stacks 题意:n次操作.模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列 思路:开三个栈,并且用到了merge函数 O(n)的复杂度 #include ...

  2. hdu 5818 Joint Stacks (优先队列)

    Joint Stacks Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

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

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

  5. 2016 多校联赛7 Elegant Construction

    Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, ...

  6. 2016 多校联赛7 Balls and Boxes(概率期望)

    Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...

  7. HDU 5818 Joint Stacks (优先队列)

    Joint Stacks 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5818 Description A stack is a data stru ...

  8. HDU 5818 Joint Stacks(联合栈)

    HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  9. HDU 5818 Joint Stacks

    Joint Stacks Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

随机推荐

  1. React Native之FlexBox布局

    参考原文链接:https://www.cnblogs.com/wujy/p/5841685.html 弹性盒模型(The Flexible Box Module),又叫Flexbox,意为“弹性布局” ...

  2. Python Microsoft Visual C++ 10.0 is required (Unable to find vcvarsall.bat)

    在windows 平台下,当python使用以下方式安装时,可能出现以下错误: > python setup.py install error: Microsoft Visual C++ 10. ...

  3. Beta阶段——第4篇 Scrum 冲刺博客

    Beta阶段--第4篇 Scrum 冲刺博客 标签:软件工程 一.站立式会议照片 二.每个人的工作 (有work item 的ID) 昨日已完成的工作 人员 工作 林羽晴 昨日完成获取提醒语句的接口函 ...

  4. Oracle 11.2.0.4.0 Dataguard部署和日常维护(6)-Dataguard Snapshot篇

    1. 检查当前主备库同步状态 on primary select ads.dest_id,max(sequence#) "Current Sequence", max(log_se ...

  5. ActiveMQ producer 流量控制

    http://activemq.apache.org/producer-flow-control.html 翻译: 流量控制是指:如果broker检测到destination的内存限制.temp文件限 ...

  6. Python学习之路【第一篇】-Python简介和基础入门

    1.Python简介 1.1 Python是什么 相信混迹IT界的很多朋友都知道,Python是近年来最火的一个热点,没有之一.从性质上来讲它和我们熟知的C.java.php等没有什么本质的区别,也是 ...

  7. AIX 5335端口IBM WebSphere应用服务器关闭连接信息泄露漏洞的修复

    今天按要求协助进行漏洞修复,有个“IBM WebSphere应用服务器关闭连接信息泄露漏洞”,一直没太搞清是不是没打补丁引起的问题. 感觉同样的安装有的报这漏洞有的不报,而报的有的是应用端口,有时是控 ...

  8. Qt画笔实现波形区域图

    参考文章:https://blog.csdn.net/yuxing55555/article/details/79752978 效果图: void WareArea::paintEvent(QPain ...

  9. Linux 硬件信息命令

    # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat /proc/cpuinfo| g ...

  10. bash 调试

    bashdb test.sh step edit  visual 跳到那一行 R restart http://www.ibm.com/developerworks/cn/linux/l-cn-she ...