暑假练习赛 004 E Joint Stacks(优先队列模拟)
Joint StacksCrawling in process... Crawling failed Time Limit:4000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
Hint
Description
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









, 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
Sample Input
4
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
Sample Output
Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0
/*今晚心事繁多,被一个E题卡了很久,心不在焉吧*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <queue>
#define N 50010
using namespace std;
struct node
{
int num;
int id;
bool operator < (const node &other) const
{
return id<other.id;
}
};
priority_queue<node>a;
priority_queue<node>b;
priority_queue<node>c;
/*
这里一定要用三个优先队列,因为当重复进行merge B A merge A B操作的时候一定会超时的(数据量太大了)
用三个优先队列的话,就能完美解决这个问题了,只要是合并的就合并到c中不管怎么进行操作,都不会再排序了
*/
int n;
char str[];//命令
int main()
{
//freopen("in.txt","r",stdin);
int p=;
while(~scanf("%d",&n)!=EOF&&n)
{
while(!a.empty()) a.pop();
while(!b.empty()) b.pop();
while(!c.empty()) c.pop();
printf("Case #%d:\n",p++);
//memset(a,0,sizeof a);
//memset(b,0,sizeof b);
for(int i=;i<n;i++)
{
scanf("%s",&str);//输入命令
//cout<<"str="<<str<<endl;
char ch;
int sh;
if(strcmp(str,"push")==)
{
//cout<<"第一个命令"<<endl;
scanf("%*c%c %d",&ch,&sh);
getchar();
//cout<<ch<<" "<<sh<<endl;
if(ch=='A')
{
node fr;
fr.num=sh;
fr.id=i;
a.push(fr);
}
else if(ch=='B')
{
node fr;
fr.num=sh;
fr.id=i;
b.push(fr);
}
//cout<<str<<" "<<ch<<" "<<sh<<endl;
}
else if(strcmp(str,"pop")==)
{
scanf("%*c%c",&ch);
getchar();
if(ch=='A')
{
if(a.empty())
{
cout<<c.top().num<<endl;
c.pop();
}
else
{
cout<<a.top().num<<endl;
a.pop();
}
}
else if(ch=='B')
{
if(b.empty())
{
cout<<c.top().num<<endl;
c.pop();
}
else
{
cout<<b.top().num<<endl;
b.pop();
}
}
//cout<<str<<" "<<ch<<endl;
}
else if(strcmp(str,"merge")==)
{
char s1,s2;
scanf("%*c%c %c",&s1,&s2);
getchar();
while(!a.empty())
{
node fr=a.top();
a.pop();
c.push(fr);
}
while(!b.empty())
{
node fr=b.top();
b.pop();
c.push(fr);
}
//cout<<str<<" "<<s1<<" "<<s2<<endl;
}
}
}
return ;
}
暑假练习赛 004 E Joint Stacks(优先队列模拟)的更多相关文章
- hdu 5818 Joint Stacks (优先队列)
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 2016暑假多校联合---Joint Stacks (STL)
HDU 5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...
- 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/Othe ...
- 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 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 多校7 HDU5818 Joint Stacks
多校7 HDU5818 Joint Stacks 题意:n次操作.模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列 思路:开三个栈,并且用到了merge函数 O(n)的复杂度 #include ...
- HDU 5818:Joint Stacks(stack + deque)
http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description A stack is a data ...
- HDU 5437 Alisha’s Party (优先队列模拟)
题意:邀请k个朋友,每个朋友带有礼物价值不一,m次开门,每次开门让一定人数p(如果门外人数少于p,全都进去)进来,当最后所有人都到了还会再开一次门,让还没进来的人进来,每次都是礼物价值高的人先进.最后 ...
随机推荐
- ①【javascript设计到的技术点】
一.dom操作: document.getElementById() document.getElementsByTagName() 二.事件操作: dom2级事件 主流浏览器 addEventLis ...
- Linux入门之常用命令(1)
退出系统 exit 图形界面 startx 时间 date 日历 cal [month] [year] 计算器 bc 帮助 man [command] // info [command] 在线用户 ...
- 终极解决方案 at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) 报空指针
java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) 出现这种问题,可能有多方 ...
- 《Go in action》读后记录:Go的并发与并行
本文的主要内容是: 了解goroutine,使用它来运行程序 了解Go是如何检测并修正竞争状态的(解决资源互斥访问的方式) 了解并使用通道chan来同步goroutine 一.使用goroutine来 ...
- Find 找规律,递推
Find Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus P ...
- 无向图广度优先遍历及其matlab实现
广度优先遍历(breadth-first traverse,bfts),称作广度优先搜索(breath first search)是连通图的一种遍历策略.之所以称作广度优先遍历是因为他的思想是从一个顶 ...
- 利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入
ASP.net Core自带DI(依赖注入),用法如下: services.AddScoped(typeof(IProductService), typeof(ProductService)); 如果 ...
- SAP 图标查找及方法
1. 图标查找 方法一:通过TCODE查找图标对应的图标名称 执行TCODE:ICON 查找图标对应的图标名称 方法二:通过方法一查出图标名称查找对应的图标ID SE11类型池根据方法一查找的ICON ...
- 输入3行字符串/定义flag/while/字符串后要加空格符
int i = 0,j = 0; for(; i < 3; i++) { gets(a[i]); }//输入3行字符串 bool flag = true; while语句的语义是:计算表达式的值 ...
- win10 uwp 获得元素绝对坐标
有时候需要获得一个元素,相对窗口的坐标,在修改他的位置可以使用. 那么 UWP 如何获得元素坐标? 我提供了一个方法,可以获得元素的坐标. 首先需要获得元素,如果没有获得元素,那么如何得到他的坐标? ...
uDebug