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 ...
随机推荐
- Java HashMap详细介绍和使用示例
①对HashMap的整体认识 HashMap是一个散列表,它存储的内容是键值对(key-value)映射. HashMap继承于AbstractMap,实现了Map.Cloneable.java.io ...
- 整理一些《纸书科学计算器》的小Tips
本文最开始是在2016年的文章 Win10应用<纸书科学计算器>更新啦! 发表之后撰写的,当时那篇文章收到了不少人点赞,应用在国内市场的日下载量也突然上涨,让我感到受宠若惊,这里要感谢Wp ...
- 在VS2012中采用C++中调用DLL中的函数(4)
转自:http://www.cnblogs.com/woshitianma/p/3683495.html 这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天 ...
- 【Beginning Python】抽象(未完)
[懒惰即是美德] 抽象意味着良好的可读性:说明你在努力做什么,而不是给出你正在如何做的细节. [抽象和结构] 程序应该是非常抽象的,就像“下载网页.计算频率.打印每个单词的频率”一样易懂.翻译成程序就 ...
- 20145331《Java程序设计》第1周学习总结
20145331<Java程序设计>第1周学习总结 教材学习内容总结 第一章 1.java的三大平台分别为java SE.java EE.java ME,其中java SE是基础. 2.j ...
- office使用技巧
一.excel 1.在空格内换行:ALT+ENTER 2.打出勾:插入->符号
- 分布式缓存DistributedCache
本文是对MR案例:Map-Join的解读. 在hadoop中,共享全局变量或全局文件的几种方法 使用Configuration的set()方法,只适合数据内容比较小的场景 将缓存文件放在HDFS上,每 ...
- Servlet3.0异步请求
在Servlet3.0之前,Servlet采用Thread-Per-Request的方式处理请求 即每次Http请求都有一个线程从头到尾负责处理 如果一个请求需要进行IO操作,比如访问数据库.调用第三 ...
- AccessToken-->Password Grant
https://www.oauth.com/oauth2-servers/access-tokens/password-grant/ The Password grant is used when t ...
- 转载:poj题目分类(侵删)
转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) ...