hdu-5818 Joint Stacks(模拟)
题目链接:
Joint Stacks
Time Limit: 8000/4000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
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.
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e18;
const int N=1e5+10;
const int maxn=5e3+4;
const double eps=1e-12; stack<int>a,b,c,d; char s[10],st[10],str[10]; LL temp[N];
int main()
{
int Case=0;
LL x;
while(1)
{
int n,cnt=0;
read(n);
if(n==0)break;
while(!a.empty())a.pop();
while(!b.empty())b.pop();
while(!c.empty())c.pop();
printf("Case #%d:\n",++Case);
For(i,1,n)
{
scanf("%s%s",s,str);
if(s[0]=='p')
{
if(s[1]=='u')
{
scanf("%lld",&x);
temp[++cnt]=x;
if(str[0]=='A')a.push(cnt);
else b.push(cnt);
}
else
{
if(str[0]=='A'&&!a.empty())
{
printf("%lld\n",temp[a.top()]);
a.pop();
}
else if(str[0]=='B'&&!b.empty())
{
printf("%lld\n",temp[b.top()]);
b.pop();
}
else
{
printf("%lld\n",temp[c.top()]);
c.pop();
}
}
}
else
{
scanf("%s",st);
while(!a.empty()||!b.empty())
{ int A,B;
if(a.empty())A=0;
else A=a.top();
if(b.empty())B=0;
else B=b.top();
if(A>B)
{
a.pop();
d.push(A);
}
else
{
b.pop();
d.push(B);
}
}
while(!d.empty())
{
c.push(d.top());
d.pop();
}
}
}
}
return 0;
}
hdu-5818 Joint Stacks(模拟)的更多相关文章
- HDU 5818 Joint Stacks(联合栈)
HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 5818 Joint Stacks (优先队列)
Joint Stacks 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5818 Description A stack is a data stru ...
- HDU 5818 Joint Stacks
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu 5818 Joint Stacks (优先队列)
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5818 Joint Stacks ——(栈的操作模拟,优先队列)
题意:有两个栈A和B,有3种操作:push,pop,merge.前两种都是栈的操作,最后一种表示的是如果“merge A B”,那么把B中的元素全部放到A中,且满足先入后出的栈原则. 分析:显然,我们 ...
- HDU - 5818 Joint Stacks 比较大の模拟,stack,erase
https://vjudge.net/problem/HDU-5818 题意:给你两个栈AB,有常规push,pop操作,以及一个merge操作,merge A B 即将A.B的元素按照入栈顺序全部出 ...
- HDU 5818 Joint Stacks(左偏树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5818 [题目大意] 给出两个栈A B(初始时为空),有三种操作: push.pop.merge. ...
- HDU 5818:Joint Stacks(stack + deque)
http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description A stack is a data ...
- 暑假练习赛 004 E Joint Stacks(优先队列模拟)
Joint StacksCrawling in process... Crawling failed Time Limit:4000MS Memory Limit:65536KB 64 ...
随机推荐
- android历史
Android一词最早是出如今法国作家维里耶德利尔·亚当1986年发表的<未来夏娃>这部科幻小说中,作者利尔·亚当将外表像人类的机器起名为Android.这就是Android小人名字的由来 ...
- SAS学习经验总结分享:篇四—SQL过程
SQL过程 SQL过程是实现对数据集或关系数据库的表进行操作的过程,对数据集或关系数据库的表进行查询.修改.创建表.删除数据.插入数据和更新数据等功能.提现了SAS对大型数据库管理系统通用的SQL语言 ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- Linux 文件系统IO性能优化
对于LINUX SA来说,服务器性能是需要我们特别关注的,包括CPU.IO.内存等等系统的优化变得至关重要,这里转载一篇非常不错的关于IO优化的文章,供大家参考和学习: 一.关于页面缓存的信息,可以用 ...
- typeof 与 instanceof 区别
typeof typeof 是一元运算符,返回值是字符串,且只能是number,string,boolean,object,function,undefined typeof用来判断一个值是否存在 i ...
- [转]Unity3D Editor 编辑器简易教程
Star 自定义编辑器简易教程 an introduction to custom editors 原文地址 http://catlikecoding.com/unity/tutorials/star ...
- 再看python多线程------threading模块
现在把关于多线程的能想到的需要注意的点记录一下: 关于threading模块: 1.关于 传参问题 如果调用的子线程函数需要传参,要在参数后面加一个“,”否则会抛参数异常的错误. 如下: for i ...
- 实现RTSP摄像机进行网页直播和微信直播的技术方案:EasyNVR自动更新方法
问题背景: 1.EasyNVR的用户越来越多,技术人员一一对应解答效率不高: 2.随着EasyNVR应用场景的不断增加,以及EasyNVR自身在技术上的不断优化,版本更新比较快: 3.由于开发人力有限 ...
- regularexpression_action
re.compile('"ssid":"[^"]*"}',re.MULTILINE) regex ,str_= re.compile('"s ...
- json字符串转集合或者数组
import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util. ...