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. C语言经典100例(51-100)

    [程序51] 题目:学习使用按位与 & . 分析:0&0=0; 0&1=0; 1&0=0; 1&1=1 #include "stdio.h" ...

  2. linux安装git服务器和svn服务器

    linux版本 linux版本为CentOS 6.8 (要注意有些软件的安装方法在各个linux版本之间也是存在差异的) git服务器 git服务器需要提供一个UI供开发人员创建项目管理项目,选择使用 ...

  3. redis 学习(6)-- 集合类型

    redis 学习(6)-- 集合类型 set 结构 无序 无重复 集合间操作 set 集合内操作 命令 含义 sadd key memebr1 [member2...] 向集合中添加一个或多个成员 s ...

  4. Eclipce远程调试

    1.注意: root权限启动的进程不支持远程调试,也有很多隐患,支持最高权限,应用普通用户启动,又原先安装Tomcat是使用的root权限,普通用户对root安装的软件没有执行权限,可以修改相应权限, ...

  5. java 给定一个日期期间 返回形如Mar 2015 3/20-3/31的数据

    最近一个项目中有个前台对于表头要求: 给定一个日期期间返回形如 Mar 2015 3/20-3/31Apr 2015 4/1-4/30 这样的月年数据,简单的写了下代码,暂时没想到更好的办法 例如传进 ...

  6. 用git创建仓库关联本地项目,又一直上传不上去

    这个不能勾,我这边勾了用VSCODE上传不了

  7. 一个密码经过多次MD5加密能否提高安全性?Java MD5盐值加解密

    什么是MD5? MD5(Message Digest Algorithm 5,信息摘要算法5),是计算机广泛使用的摘要算法(又称哈希算法)之一.MD5是将一段信息,通过其不可逆的字符串变换算法,产生了 ...

  8. OpenSSL(1)密钥和证书管理

    OpenSSL是一个开源项目,包括密码库和SSL/TLS工具集. 从项目的官方站点可以看到: OpenSSL项目是安全套接字层( secure sockets layer, SSL)和传输层安全( t ...

  9. LVS DR模型RS端修改配置脚本

    #!/bin/bash vip=x.x.x.x in start) > /proc/sys/net/ipv4/conf/all/arp_ignore > /proc/sys/net/ipv ...

  10. web攻击日志分析之新手指南

    0x00 前言 现实中可能会经常出现web日志当中出现一些被攻击的迹象,比如针对你的一个站点的URL进行SQL注入测试等等,这时候需要你从日志当中分析到底是个什么情况,如果非常严重的话,可能需要调查取 ...