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,表示两个栈的栈顶,建大根的左偏树. 接下来的插入,删除,两个栈合并都是左偏树的 ...
随机推荐
- Charles篡改后台数据
1.打开Charles找到相对应的接口 2.在接口上右键选择Breakpoints打断点. 3.重新请求截断接口,Charles会多弹出一个截断窗口如下图(第一个修改请求的,第二图是修改返回的) 点击 ...
- React Native笔记
① 做UI快 ② 还是有很多限制,不如原生Native ③ 入门简单,能让前端快速开发App ④ iOS&Android大部分代码通用 ⑤ code-push能做热更新,但是用不好依旧坑
- 异步、非阻塞和IO多路复用总结
Nginx是并发处理框架的代表者,很多后台业务都会放在Nginx容器中运行,以实现高吞吐,而Nginx能够支持高并发也是由于使用了异步非阻塞处理模型,本文将用通俗的话讲解异步.同步.阻塞.非阻塞的区别 ...
- Python设计模式 - UML - 组合结构图(Composite Structure Diagram)
简介 组合结构图用来显示组合结构或部分系统的内部构造,包括类.接口.包.组件.端口和连接器等元素,是UML2.0的新增图. 组合结构图侧重复合元素的方式展示系统内部结构,包括与其他系统的交互接口和通信 ...
- CentOS7+CDH5.14.0安装CDH错误排查:Hue错误: Load Balancer 该角色的进程启动失败
Hue错误: Load Balancer 该角色的进程启动失败 解决办法:主机能够联网情况下,直接运行如下命令即可在线安装openssl.httpd 需要提前安装环境 httpd, mod_ssl ...
- oracle 锁表sql 解锁
1.select * from v$locked_object; 查看具体的 : select session_id , oracle_username, process from v$loc ...
- python requests库爬取网页小实例:爬取网页图片
爬取网页图片: #网络图片爬取 import requests import os root="C://Users//Lenovo//Desktop//" #以原文件名作为保存的文 ...
- PyCharm中 Django1.11配置Mysql数据库
1.Django 中配置MySQL数据库 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '数据库名称 ...
- 《Oracle查询优化改写技巧与案例》学习笔记-------使用数字篇
一个系列的读书笔记,读的书是有教无类和落落两位老师编写的<Oracle查询优化改写技巧与案例>. 用这个系列的读书笔记来督促自己学习Oracle,同时,对于其中一些内容,希望大家看到以后, ...
- vue项目中编写一个图片预览的公用组件
今天产品提出了一个查看影像的功能需求. 在查看单据的列表中,有一列是影像字段,一开始根据单据号调用接口查看是否有图片附件,如果有则弹出一个全屏的弹出层,如果没有给出提示.而且,从列表进入详情之后,附件 ...