hdu 5818 Joint Stacks (优先队列)
Joint Stacks
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1819 Accepted Submission(s): 819
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
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0
#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 (优先队列)的更多相关文章
- 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 ——(栈的操作模拟,优先队列)
题意:有两个栈A和B,有3种操作:push,pop,merge.前两种都是栈的操作,最后一种表示的是如果“merge A B”,那么把B中的元素全部放到A中,且满足先入后出的栈原则. 分析:显然,我们 ...
- HDU 5818 Joint Stacks(左偏树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5818 [题目大意] 给出两个栈A B(初始时为空),有三种操作: push.pop.merge. ...
- 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(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 ...
- 2016暑假多校联合---Joint Stacks (STL)
HDU 5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...
随机推荐
- Winter-1-B Sum 解题报告及测试数据
Time Limit:500MS Memory Limit:32768KB Description Hey, welcome to HDOJ(Hangzhou Dianzi University O ...
- JS监听checkbox的选择获取取消事件代码案列
function OncheckBox(index){ if ($(index).attr("checked") == "checked") { alert($ ...
- session与cookie的详解
在PHP面试中 经常碰到请阐述session与cookie的区别与联系,以及如何修改两者的有效时间. 大家都知道,session是存储在服务器端的,cookie是存储在客户端的,session依赖于c ...
- Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation
1.主要完成的任务是能够将英文转译为法文,使用了一个encoder-decoder模型,在encoder的RNN模型中是将序列转化为一个向量.在decoder中是将向量转化为输出序列,使用encode ...
- maven常见指令和插件
总结自:https://www.cnblogs.com/ysocean/p/7416307.html#_label1及 https://blog.csdn.net/zhaojianting/artic ...
- 对linux内核创建flash上的各分区源码进行分析
1.注意:内核源码版本为4.9 2.首先注意关键字符串"partitions found on MTD device 这句话在drivers/mtd/mtdpart.c的parse_mtd_ ...
- .Net Core Linux部署之进程守护 Supervisor 安装配置
1.Supervisor 安装 //安装easy_install yum install python-setuptools //安装Supervisor easy_install superviso ...
- Springboot-mongodb MongoRepository接口 save方法 详解
问题: 我们都知道 mongodb 有两种添加数据的方式 一种 就是 save 方法 另外一种 insert 方法 这里两个方法 唯一的区别就是 insert:当主键"_id&qu ...
- spring boot 使用拦截器,注解 实现 权限过滤
http://www.cnblogs.com/zhangXingSheng/p/7744997.html spring boot 使用拦截器 实现 用户登录拦截 http://www.cnblogs. ...
- Impala 技术点梳理
1.优点 1.1 快! 主节点生成执行计划树并分发执行计划至各节点并行执行的拉式获取数据(MR:推式获取数据) 计算的中间结果不写入磁盘 1.2 便利 提供SQL语义,可以方便的进行复杂的数据分析任 ...