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 (优先队列)的更多相关文章

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

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

  2. HDU 5818 Joint Stacks(联合栈)

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

  3. HDU 5818 Joint Stacks

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

  4. HDU 5818 Joint Stacks ——(栈的操作模拟,优先队列)

    题意:有两个栈A和B,有3种操作:push,pop,merge.前两种都是栈的操作,最后一种表示的是如果“merge A B”,那么把B中的元素全部放到A中,且满足先入后出的栈原则. 分析:显然,我们 ...

  5. HDU 5818 Joint Stacks(左偏树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5818 [题目大意] 给出两个栈A B(初始时为空),有三种操作: push.pop.merge. ...

  6. HDU - 5818 Joint Stacks 比较大の模拟,stack,erase

    https://vjudge.net/problem/HDU-5818 题意:给你两个栈AB,有常规push,pop操作,以及一个merge操作,merge A B 即将A.B的元素按照入栈顺序全部出 ...

  7. HDU 5818:Joint Stacks(stack + deque)

    http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description   A stack is a data ...

  8. 暑假练习赛 004 E Joint Stacks(优先队列模拟)

    Joint StacksCrawling in process... Crawling failed Time Limit:4000MS     Memory Limit:65536KB     64 ...

  9. 2016暑假多校联合---Joint Stacks (STL)

    HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...

随机推荐

  1. composer命令介绍之install和update及其区别

    composer 是 php 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 然而,对于如何『安装他们』,新手可能并不清楚.网上的答案有的说 composer in ...

  2. php7.2.1 安装

    yum -y install wget openssl* gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype ...

  3. Volatile可见性 与 Synchronization原子性的优化

    Volatile可见性 比如现在我们有这样一段代码:线程等待另一个线程将数据装载完就输出success,可是最后程序一直卡在while循环里没有往下执行. public class VolatileD ...

  4. SQL数据库字段数据类型详细说明

    这里先总结数据类型.MySQL中的数据类型大的方面来分,可以分为:日期和时间.数值,以及字符串.下面就分开来进行总结. 日期和时间数据类型 MySQL数据类型 含义 date 3字节,日期,格式:20 ...

  5. linux复习5

    权限----------------- r //100 = 4 //文件 :读取内容, //文件夹:是查看文件夹的内容 w //文件 :写数据到文件 //文件夹:增删文件. //10 = 2 x // ...

  6. python--命令(各个模块的安装)

    python命令行 退出python命令行:exit() 安装pymysql pip install pymysql 安装request pip install requests 1.安装django ...

  7. Java定义栈结构,实现入栈、出栈操作

    package com.example.demo; import java.util.ArrayList; public class Stack { ArrayList<Object> l ...

  8. 64位Win7安装Oracle12C临时位置权限错误解决方案

    今天装备安装Oracle12C体验一下,结果遇到问题:请确保当前用户具有访问临时位置所需的权限,无法继续安装,上网查了一下,解决方案如下:  第一步:  控制面板>所有控制面板项>管理工具 ...

  9. 关于windows下无法删除文件,需要TrueInstaller权限的问题

    笔者办公室的笔记本今天突然弹出来一个ie浏览器,这不是为了下载其他浏览器而存在的浏览器吗?现在还臭不要脸的弹出来,然鹅我在删除文件夹的时候,提示我无法删除,必须要有TrueInstaller的权限,那 ...

  10. centos7搭建activemq服务

    一.下载安装jdk 下载 jdk-8u211-linux-x64.rpm安装: yum -y install jdk-8u211-linux-x64.rpm 二.官网下载 activemq 软件包 官 ...