Joint Stacks

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 828    Accepted Submission(s): 403

Problem 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<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.
 
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
 
Author
SYSU
 
Source
 
 
 
解析:模拟。为了操作方便,引入栈C。每次合并的时候把A和B合并到C上,然后把A和B都清空。push操作不变,但进行pop操作时,如果pop的栈为空,改为对栈C进行操作。这样做的原因是题目保证不会对空栈进行pop操作,且pop操作进行后,pop出的元素不会再用到。
 
 
 
 
#include <cstdio>
#include <algorithm>
using namespace std; const int MAXN = 1e5+5;
int x[MAXN]; //记录数值
int s[3][MAXN]; //三个栈(s[0][]代表A, s[1][]代表B, s[2]代表C)记录时间戳,即x[]的下标
int top[3]; //三个栈的栈顶
int n; void solve()
{
char op[10], obj[5];
top[0] = top[1] = top[2] = 0;
for(int i = 0; i < n; ++i){
scanf("%s%s", op, obj);
int oo = obj[0]-'A';
if(op[1] == 'u'){
scanf("%d", &x[i]);
s[oo][top[oo]++] = i; //记录时间戳i,对应栈顶++
}
else if(op[1] == 'o'){
if(top[oo] == 0) //要pop的栈为空时,改为对C栈进行操作
oo = 2;
printf("%d\n", x[s[oo][--top[oo]]]);
}
else{
scanf("%s", obj);
//合并栈A和栈B放到栈C(合并的是时间戳)
top[2] = merge(s[0], s[0]+top[0],
s[1], s[1]+top[1],
s[2]+top[2]) - s[2];
top[0] = top[1] = 0;
}
}
} int main()
{
int cn = 0;
while(scanf("%d", &n), n){
printf("Case #%d:\n", ++cn);
solve();
}
return 0;
}

  

HDU 5818 Joint Stacks的更多相关文章

  1. HDU 5818 Joint Stacks(联合栈)

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

  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(左偏树)

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

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

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

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

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

  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. 如何有效地记录 Java SQL 日志?

    在常规项目的开发中可能最容易出问题的地方就在于对数据库的处理了,在大部分的环境下,我们对数据库的操作都是使用流行的框架,比如 Hibernate . MyBatis 等.由于各种原因,我们有时会想知道 ...

  2. POJ3414Pots

    http://poj.org/problem?id=3414 题意 : 大意是说给你两个杯子的体积和一个目标体积,a,b,c,通过对a,b进行6种操作,调出c体积的水,6种操作分别是把a倒满,把b倒满 ...

  3. java.util.concurrent包API学习笔记

    newFixedThreadPool 创建一个固定大小的线程池. shutdown():用于关闭启动线程,如果不调用该语句,jvm不会关闭. awaitTermination():用于等待子线程结束, ...

  4. 查看服务器的TCP/IP(http)连接情况

    6.那具体到,怎样监听一个具体程序的TCP/IP连接情况呢???!! 就要用:netstat -n|grep 80 命令来查看具体端口对应程序的TCP/IP连接情况了 netstat -n|grep ...

  5. SQL查询(笔记2——实体查询)

    SQL查询(笔记2——实体查询) 二.实体查询 如果查询返回了某个数据表的全部数据列,且该数据表有对应的持久化类映射,我们就把查询结果转换成实体查询.将查询结果转换成实体,可以使用SQLQuery提供 ...

  6. django定期执行任务

    要在django项目中定期执行任务,比如每天一定的时间点抓取数据,刷新数据库等,可以参考stackoverflow的方法,先编写一个manage.py命令,然后使用crontab来定时执行这个命令. ...

  7. Django admin的一些有用定制

    Model实例,myapp/models.py: from django.db import models class Blog(models.Model): name = models.CharFi ...

  8. API设计

    ---恢复内容开始--- 参考:http://www.cnblogs.com/youxin/p/3967274.html http://scotch.io/tutorials/simple-larav ...

  9. 自定义View(2)canas绘制基本图形的示例

    效果 代码: void drawSample(Canvas canvas) { /* * 方法 说明 drawRect 绘制矩形 drawCircle 绘制圆形 drawOval 绘制椭圆 drawP ...

  10. [HDOJ2874]Connections between cities(LCA, 离线tarjan)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 这题有不连通的情况,特别注意. 觉得是存query的姿势不对,用前向星存了一遍,还是T…… /* ...