HDU 5818 Joint Stacks (优先队列)
Joint Stacks
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5818
Description
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.
Input
There are multiple test cases. For each case, the first line contains an integer N(0
Output
For 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
Source
2016 Multi-University Training Contest 7
##题意:
给出两个栈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).
官方题解说的是直接用三个栈来模拟,那么对于merge操作时,要用双指针比较来决定入栈顺序.
题解还说可以用链表模拟,遗憾的是一开始就用std::list来做,然后莫名RE....
(想太复杂了,瞎搞了两个小时).
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 1010
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;
typedef pair<int,int> pii;
priority_queue com;
priority_queue a;
priority_queue b;
int main(int argc, char const *argv[])
{
//IN;
int n; int ca = 1;
while(scanf("%d", &n) != EOF && n)
{
printf("Case #%d:\n", ca++);
while(!a.empty()) a.pop();
while(!b.empty()) b.pop();
while(!com.empty()) com.pop();
int time_cnt = 0;
while(n--) {
char op[10]; char aim;
scanf("%s %c", op,&aim);
if(op[1] == 'u') {
int x; scanf("%d", &x);
if(aim == 'A') {
a.push(make_pair(time_cnt++, x));
} else {
b.push(make_pair(time_cnt++, x));
}
}
else if(op[1] == 'o') {
if(aim == 'A') {
if(!a.empty()) {
pii x = a.top(); a.pop();
printf("%d\n", x.second);
} else {
pii x = com.top(); com.pop();
printf("%d\n", x.second);
}
} else {
if(!b.empty()) {
pii x = b.top(); b.pop();
printf("%d\n", x.second);
} else {
pii x = com.top(); com.pop();
printf("%d\n", x.second);
}
}
}
else {
char tmp[10]; gets(tmp);
while(!a.empty()) {
pii x = a.top();
a.pop();
com.push(x);
}
while(!b.empty()) {
pii x = b.top();
b.pop();
com.push(x);
}
}
}
}
return 0;
}
HDU 5818 Joint Stacks (优先队列)的更多相关文章
- 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(联合栈)
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 ——(栈的操作模拟,优先队列)
题意:有两个栈A和B,有3种操作:push,pop,merge.前两种都是栈的操作,最后一种表示的是如果“merge A B”,那么把B中的元素全部放到A中,且满足先入后出的栈原则. 分析:显然,我们 ...
- HDU 5818 Joint Stacks(左偏树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5818 [题目大意] 给出两个栈A B(初始时为空),有三种操作: push.pop.merge. ...
- HDU - 5818 Joint Stacks 比较大の模拟,stack,erase
https://vjudge.net/problem/HDU-5818 题意:给你两个栈AB,有常规push,pop操作,以及一个merge操作,merge A B 即将A.B的元素按照入栈顺序全部出 ...
- HDU 5818:Joint Stacks(stack + deque)
http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description A stack is a data ...
- 暑假练习赛 004 E Joint Stacks(优先队列模拟)
Joint StacksCrawling in process... Crawling failed Time Limit:4000MS Memory Limit:65536KB 64 ...
- 2016暑假多校联合---Joint Stacks (STL)
HDU 5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...
随机推荐
- java多线程之并发编程
1.并发不一定比串行更快 因为并发有线程创建和上下文切换的开销 2.java的并发采用内存共享模型 3.单线程中重排序不会影响到结果 但多线程中重排序可能会影响到结果 4.votaile变量 当线程A ...
- ajax实现异步操作实例1
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 教你用python爬取网站美女图(附代码及教程)
我前几篇文章都是说一些python爬虫库的用法,还没有说怎样利用好这些知识玩一些好玩的东西.那我今天带大家玩好玩又刺激的,嘻嘻!对了,requests库和正则表达式很重要的,一定要学会!一定要学会!! ...
- python之BeautifulSoup4
阅读目录 1.Beautiful Soup4的安装配置 2.BeautifulSoup的基本用法 (1)节点选择器(tag) (2)方法选择器 (3)CSS选择器 (4)tag修改方法 Beautif ...
- javaScript基本使用api
基本方法 isArray() 判断数组 isArray() 方法用于判断是否是数组(有兼容性) 语法:Array.isArray(arr) 返回值:是数组,返回true.不是数组,返回false. i ...
- Google浏览器显示URL的 http https ....
谷歌浏览器输入 chrome://flags/#omnibox-ui-hide-steady-state-url-trivial-subdomains 输入之后, 高亮部分选项 改为 Disabled ...
- Nginx,LVS,HAProxy详解
Nginx/LVS/HAProxy负载均衡软件的优缺点详解 PS:Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,参考了一些资料,结合自己的一些使用经验 ...
- Python测开面试题之装饰器
Python的装饰器是面试常被问到的问题之一,在面试Python测试开发时被问到的概率不低于70%,那么装饰器的原理是什么,怎么快速写出一个装饰器呢,接下来我们详细讲解装饰器的实现方法. Python ...
- docker容器内安装 rz、sz
操作系统:ubuntu rz.sz命令找不到: 执行命令:apt-get update && apt-get install lrzsz
- 【LOJ 6695】天气之子
找规律题的典范? OEIS裸题 考场上让你用 OEIS 吗 题意 link 题解 \(n\le 5\) 打表 \(n\le 10^5\) 发现不能直接求最优解,于是二分答案. 验证答案时,先把前 \( ...