HDU5818 Joint Stacks
Joint Stacks
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1815 Accepted Submission(s): 815
(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.
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.
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
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0
—————————————————————————————————
题目的意思给出两个栈A B(初始时为空),有三种操作:
push、pop、merge.
其中merge是按照A B中元素进栈的相对顺序来重排的.
思路:因为题目说了不会对空栈弹出,所以开三个优队(栈也可以)前两个分别存A和B,第三个公用,插入删除还是对AB操作,合并把AB情况放到C中,每次输入时先判AB是否是空,不为空则输出AB顶,否则这个元素肯定在C中输出即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; int n,x;
char s1[10],s2[5],s3[5];
int cnt [1000005],fl[1000006];
struct node
{
int t,val;
friend bool operator <(const node &a,const node &b)
{
return a.t<b.t;
}
} pre; int main()
{
int cas=0;
while(~scanf("%d",&n)&&n)
{
printf("Case #%d:\n",++cas);
priority_queue<node>q1,q2,q3;
for(int i=1; i<=n; i++)
{
scanf("%s",s1);
if(!strcmp(s1,"push"))
{
scanf("%s%d",s2,&x);
pre.t=i,pre.val=x;
if(!strcmp(s2,"A")) q1.push(pre);
else q2.push(pre);
}
else if(!strcmp(s1,"pop"))
{
scanf("%s",s2);
if(!strcmp(s2,"A"))
{
if(!q1.empty())
printf("%d\n",q1.top().val),q1.pop();
else
printf("%d\n",q3.top().val),q3.pop();
}
else
{
if(!q2.empty())
printf("%d\n",q2.top().val),q2.pop();
else
printf("%d\n",q3.top().val),q3.pop();
}
}
else
{
scanf("%s%s",s2,s3);
while(!q2.empty())
q3.push(q2.top()),q2.pop();
while(!q1.empty())
q3.push(q1.top()),q1.pop();
}
}
}
return 0;
}
HDU5818 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/Othe ...
- HDU5818 Joint Stacks 左偏树,可并堆
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU5818 题意概括 有两个栈,有3种操作. 第一种是往其中一个栈加入一个数: 第二种是取出其中一个栈的顶 ...
- HDU 5818:Joint Stacks(stack + deque)
http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description A stack is a data ...
- 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 ...
- hdu 5818 Joint Stacks (优先队列)
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5818 Joint Stacks (优先队列)
Joint Stacks 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5818 Description A stack is a data stru ...
- 【XSY2488】【HDU5818】Joint Stacks
这题合并栈让我们想到了左偏树. 我们可以维护val值为时间,dis值为size的左偏树,定义两个根root1和root2,表示两个栈的栈顶,建大根的左偏树. 接下来的插入,删除,两个栈合并都是左偏树的 ...
随机推荐
- Oracel递归查询start with ...connect by prior在Mysql中的实现
Oracle是一个强大的数据库,有很多的函数和语法可以带来很多便利,有些函数和语法在Mysql中有代替的,但是有些没有现成可用的方法,比如Oracle的递归,在Mysql中怎么实现呢? 例子: Ora ...
- d3.js d3.transform 方法移除的解决方案
rt d3.transform在新版本中移除 需要自行写出该功能 function getTranslation(transform) { // Create a dummy g for calcul ...
- Linux - 其他命令
其他命令 目标 查找文件 find 软链接 ln 打包和压缩 tar 软件安装 apt-get 01. 查找文件 find 命令功能非常强大,通常用来在 特定的目录下 搜索 符合条件的文件 序号 命令 ...
- POJ-3159.Candies.(差分约束 + Spfa)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 40407 Accepted: 11367 Descri ...
- 有关Lambda的一些思考
问题: What do lambda expressions do? Can we write all functions as lambda expressions? In what cases a ...
- ROS零门槛学渣教程系列(二十)——ROSJAVA和Android
ros wiki地址:http://wiki.ros.org/android 第一步:安装JDK (参考博文:https://blog.csdn.net/Hong_A/article/details/ ...
- jmeter导入DB数据再优化
由于同一个迭代中每天都在执行.之前设计的思路是同个迭代只执行一次插入DB操作!! 因而没有在插入数据前没有做版本.产品类型.页面类型.接口名.接口名是否相等判断操作. 因此,若是这些条件相等,数据不是 ...
- Asp.net core 向Consul 注册服务
Consul服务发现的使用方法:1. 在每台电脑上都以Client Mode的方式运行一个Consul代理, 这个代理只负责与Consul Cluster高效地交换最新注册信息(不参与Leader的选 ...
- 除非你是BAT,前端开发中最好少造轮子
站在前人的肩膀上 HTML.CSS.JavaScript是前端的根基,这是无可否认的事实.正如一辆车当然都是由一堆钢板和螺钉组成的,但是现在还有人拎着个锤子敲敲打打的造车吗?李书福说过,“汽车不过是四 ...
- [DP][NOIP2013]花匠
花匠 问题描述: 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希望剩下的花排列得比较别致. ...