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. iOS 使用node js 搭建简单的本地服务器

    一.前提:基于iOS 项目 调用,使用了第三方框架NodeMobile.技术说明关键是 应用生命整个周期只能在应用启动时候开辟的一个线程里申请 一个 node  js 资源.如果终止了运行,重启是不支 ...

  2. python3_unittest单元测试框架

    看见英文懵逼,强迫学习英语 The Unittest suppots test automation,sharing of setup and shutdown code of tests, aggr ...

  3. C++ 第三十四天

    c++ 已经搁了很久了,之所以捡起来是因为学校数据结构课程设置 **.我对 c++ 的掌握非常不扎实,因为除了顺序阅读 c++ primer 外就没有什么实践, 但是我又无法忍受自己写出来的 * 一样 ...

  4. javascript 面向对象 new 关键字 原型链 构造函数

    JavaScript面向对象JavaScript 语言使用构造函数(constructor)作为对象的模板.所谓"构造函数",就是专门用来生成实例对象的函数.它就是对象的模板,描述 ...

  5. Java-Minor GC、Major GC、Full GC

    Minor GC: 回收年轻代(Young)空间,包括Eden区.Survivor区. JVM无法为一个新对象分配空间时,比如eden区满了,就会触发Minor GC. Major GC: 清理永久代 ...

  6. mybatis家族

    mybatis 优秀的持久层框架,它支持定制化SQL.存储过程以及高级映射. 备注:通过mapper实现数据库与实体类相互映射 MyBatis 避免了几乎所有的JDBC 代码和手动设置参数以及获取结果 ...

  7. Vue.js项目部署在Tomcat服务器上

    1.在本地的Vue框架中 执行npm run build  将我们的项目打包到dist 文件夹中 2.在服务器上的Tomcat的 webapps文件夹下,新建一个文件夹如:frontvue 3.启动t ...

  8. apache配置ssl

    1.确认是否安装ssl模块 是否有mod_ssl.so文件   2.生成证书和密钥   linux下 步骤1:生成密钥 命令:openssl genrsa 1024 > server.key 说 ...

  9. mac 下 安装 mongodb

    使用brew安装,不过brew不再更新, 通过 sudo chown -R $(whoami):admin /usr/local 这条语句终端有提醒的 xcode-select --install 需 ...

  10. vim与shell切换

    扩展一些vim的知识. vim与shell切换 :shell 可以在不关闭vi的情况下切换到shell命令行. :exit 从shell回到vim. 文件浏览 :Ex 开启目录浏览器,可以浏览当前目录 ...