HDU 5818 Joint Stacks联合栈

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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.

栈是一种数据结构,仅允许在其中一端进行插入和删除,这一端被称为栈顶。最后入栈的元素将会最先出栈。换句话说,这个操作就是后进先出(LIFO)。

一个可合并的栈拥有"merge"操作。有如下三种操作:

- push A x: 将 x 插入栈 A

- pop A: 弹出 A 的栈顶元素

- merge A B: 合并栈 A 与 B

执行"merge A B"操作后,栈A将获得B中全部元素,随后B则为空。新栈中的元素根据先前的入栈时间重新排列,如同再执行他们的"push"操作到一个栈。参考输入/输出样例的详细说明。

给定两个可合并栈A与B,执行上述操作。

Input

输入

There are multiple test cases. For each case, the first line contains an integer N(0<N≤105), 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.

多组测试用例。对于每个测试用例,第一行有一个整数N(0<N≤105),表示操作的数量。随后N行,每行包含一个"push"、 "pop"或"merge"指令。栈中的元素都是32位整数。保证"pop"操作不会出现在空栈中。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.

对于每个用例,输出一行"Case #t:",t表示用例编号(从1开始)。对于每个"pop"操作,输出出栈的元素在单独一行。

Sample Input - 输入样例

Sample Output - 输出样例

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

Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0

【题解】

使用三个优先队列,一个给A,一个B,一个给合并的部分。

当A/B为空的时候,就从合并的部分取。

只用AB的话,感觉就不靠谱,懒癌发作,不想试。

【代码 C++】

 #include <cstdio>
#include <queue>
#define mx 100005
int data[mx], id;
int main(){
int t = , n, x;
char op[], o;
while (scanf("%d", &n), n){
printf("Case #%d:\n", t++); id = -;
std::priority_queue<int, std::vector<int> > a, b, m; while (n--){
scanf("%s", op); getchar();
if (op[] == 'u'){
scanf("%c%d", &o, &data[++id]);
if (o == 'A') a.push(id);
else b.push(id);
}
else if (op[] == 'o'){
if (getchar() == 'A'){
if (a.empty()) x = data[m.top()], m.pop();
else x = data[a.top()], a.pop();
}
else{
if (b.empty()) x = data[m.top()], m.pop();
else x = data[b.top()], b.pop();
}
printf("%d\n", x);
}
else{
for (gets(op); !a.empty(); a.pop()) m.push(a.top());
while (!b.empty()) m.push(b.top()), b.pop();
}
}
}
return ;
}

HDU 5818 Joint Stacks(联合栈)的更多相关文章

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

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

  2. HDU 5818 Joint Stacks (优先队列)

    Joint Stacks 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5818 Description A stack is a data stru ...

  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 (优先队列)

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

  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. 2016暑假多校联合---Joint Stacks (STL)

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

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

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

随机推荐

  1. lamp环境编译(实际通过)

    CentOS:5.5Apache:2.2.9PHP:5.2.6MySQL:5.0.41 条件: yum install gccyum install gcc-c++ vi/etc/yum.repos. ...

  2. 【python cookbook】【字符串与文本】15.给字符串中的变量名做插值处理

    问题:想创建一个字符串,其中嵌入的变量名称会以变量的字符串值形式替换掉 解决方法:str.format().str.format_map() >>> s = '{name} has ...

  3. 161123、ssh scp 复制文件和文件夹

    复制文件或目录命令:  复制文件:  (1)将本地文件拷贝到远程  scp 文件名用户名@计算机IP或者计算机名称:远程路径 本地192.168.80.100客户端  scp /root/instal ...

  4. ESP8266例程

    乐鑫的这个开发板, 可以用LUA来编程, 下面的例子是一个简单的web服务器, 当你用浏览器访问wifi的IP时,后面加一个http://ip/abc123, 这样就可以给wifi模组发命令了. sr ...

  5. powershell: 生成随机字符串

    ASCII范围内的 获取6个随机字符(字母和数字) 48到57是数字0-9,powershell的范围操作符是..,和Perl 5的一样, 所以 48..57就是指(48 49 50 51 52 53 ...

  6. nginx完美支持tp框架

    nginx完美支持tp框架 server { listen 80; server_name mit.520m.com.cn; access_log /data/wwwlogs/mit.520m.com ...

  7. C#:反射

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. RMAN笔记小结

    首先感谢junsansi和leshani两位大师的文笔:我可是好好看了很多遍:这两位哦:我跟你们说:对rman的解说那是相当精彩:文章形如流水,通俗易懂:内容那是入木三分...;好了我也不多说了,反正 ...

  9. Serializable接口使用纪实

    这两天依领导要求使用sonar工具测试了一下项目代码,其中有一个问题是 而这个类的结构大概是这样的: public class Demo<T> implements Serializabl ...

  10. python 拷贝文件夹下所有的文件到指定文件夹(不包括目录)

    1.随便简单些写了一下.直接粘结代码,只是简单的实现一下,还很多需要完善和扩展的地方,比如忽略掉后缀文件,删除文件 如果排除的某些的话可以用: sourceF.find('.后缀')>0 2.注 ...