Joint Stacks

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

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 

push A 0 
push A 1 
push B 3 
pop A 
push A 2 
merge A B 
pop A 
pop A 
pop A 

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: 


Case #2: 




Case #3: 



0
 
Source
 
题解一:
因为merge操作的耗时多,如果纯模拟会tle,所以我们利用滚动数组,在merge的时候,把少的合并到多的地方。
#include <iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
char ch[],str1[],str2[];
int n,x,id,id1,id2; struct node
{
int k,ti;
node(int x,int y){k=x; ti=y;}
}; struct cmp
{
bool operator()(node a,node b)
{
return a.ti<b.ti;
}
}; int main()
{
int cas=;
while(~scanf("%d",&n))
{
if (n==) break;
printf("Case #%d:\n",++cas);
priority_queue<node,vector<node>,cmp> a[];
int flag=;
for(int i=;i<=n;i++)
{
scanf("%s",&ch);
if (ch[]=='u')
{
scanf("%s%d",&str1,&x);
id=str1[]-'A'+;
id=(id+flag)%;
a[id].push(node(x,i));
} else
if (ch[]=='o')
{
scanf("%s",&str1);
id=str1[]-'A'+;
id=(id+flag)%;
printf("%d\n",a[id].top());
a[id].pop();
} else
{
scanf("%s%s",&str1,&str2);
id1=str1[]-'A'+; id1=(id1+flag)%;
id2=str2[]-'A'+; id2=(id2+flag)%;
if (a[id1].size()>=a[id2].size())
{
while(!a[id2].empty())
{
a[id1].push(a[id2].top());
a[id2].pop();
}
} else
{
while(!a[id1].empty())
{
a[id2].push(a[id1].top());
a[id1].pop();
}
flag=(flag+)%;
}
}
}
}
return ;
}

题解二:

充分利用题目中的说明:“不会pop空了的栈”,这样我们可以开三个优先队列。

#include <iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std; struct node
{
int k,ti;
node(int x,int y){k=x; ti=y;}
};
struct cmp
{
bool operator()(node a,node b)
{
return a.ti<b.ti;
}
};
char ch[],str1[],str2[];
int n,x; int main()
{
int cas=;
while(~scanf("%d",&n))
{
if (n==) break;
printf("Case #%d:\n",++cas);
priority_queue<node,vector<node>,cmp> A,B,C;
for(int i=;i<=n;i++)
{
scanf("%s",&ch);
if (ch[]=='u')
{
scanf("%s%d",&str1,&x);
if (str1[]=='A') A.push(node(x,i));
else B.push(node(x,i));
} else
if (ch[]=='o')
{
scanf("%s",&str1);
if(str1[]=='A')
{
if(!A.empty()) {printf("%d\n",A.top().k); A.pop();}
else {printf("%d\n",C.top().k); C.pop();}
} else
{
if(!B.empty()) {printf("%d\n",B.top().k); B.pop();}
else {printf("%d\n",C.top().k); C.pop();}
}
} else
{
scanf("%s%s",&str1,&str2);
while(!A.empty())
{
C.push(A.top());
A.pop();
}
while(!B.empty())
{
C.push(B.top());
B.pop();
}
}
}
}
return ;
}

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 ——(栈的操作模拟,优先队列)

    题意:有两个栈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. spark restful 作业提交

    spark1.4起,在启动master进程时候,同时会有一个restful的服务器,可以接受RESTFUL的请求, 以下是提交应用的示例 curl -X POST http://tssloginsig ...

  2. bind函数的作用

    面向连接的网络应用程序分为客户端和服务器端.服务器端的执行流程一般为4步,客户端程序相对简单,一般需要两个步骤. 服务器端执行流程4步如下: (1)调用socket函数,建立一个套接字,该套接字用于接 ...

  3. Spring Boot+BootStrap fileInput 多图片上传

    一.依赖文件 <link rel="stylesheet" type="text/css" th:href="@{/js/bootstrap/c ...

  4. 微服务-使用Redis实现分布式缓存

    在单体中对于key信息和用户信息是放在内存中放的,通过session进行管理. 微服务是要放在分布式缓存中,以实现服务的无状态化. @Autowired private StringRedisTemp ...

  5. SQL统计信息解释

    [SQL基础]统计信息解释 在平时优化SQL的时候,最长用的就是:SET STATISTICS ON,它可以用来查看我们写的查询语句到底性能如何,不过,究竟这个性能的指标是怎么样的呢?首先需要明白的, ...

  6. 20145216史婧瑶《Java程序设计》第一次实验报告

    实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没 ...

  7. mybatis动态插入数据(使用trim标签)

    知识点: 当向一张表里插入数据,传入的参数不固定时,使用到mybatis的动态插入(trim标签) 参考博客:https://blog.csdn.net/h12kjgj/article/details ...

  8. String中的equals方法解析 jdk1.7

    注  此篇为jdk1.7中的源码解析 equals()方法中的判断分一下步骤 1先判断内存地址是否相同  如果内存地址相同 那么字符串就是相同的 返回true 2 判断当前字符串和参数字是否属于同一类 ...

  9. HTML中table的td宽度无法固定问题

    设置了 width="10%" 依然会被内容撑大, 加了 style="word-break:break-all;" 属性就好了.效果是内容自动回车. 此属性不 ...

  10. POJ 1236 Network of School

    http://poj.org/problem?id=1236 题意: 给出一个图,至少要选多少个点才能遍历全图和至少需要添加多少边使得整个图是强连通. 思路: 强连通计算连通分量后缩点,计算入度为0的 ...