暑假练习赛 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,全都进去)进来,当最后所有人都到了还会再开一次门,让还没进来的人进来,每次都是礼物价值高的人先进.最后 ...
随机推荐
- vim/network/ssh
一.编辑器--vim vi编辑器是Linux和Unix上最基本的文本编辑器,工作在字符模式下.由于不需要图形界面,vi是效率很高的文本编辑器.尽管在Linux上也有很多图形界面的编辑器可用,但vi在系 ...
- 基于c编写的关于随机生成四则运算的小程序
基于http://www.cnblogs.com/HAOZHE/p/5276763.html改编写的关于随机生成四则运算的小程序 github源码和工程文件地址:https://github.com/ ...
- 多年iOS开发经验总结
总结了几个月的东西终于能和大家分享了,不多说,直接看东西! 1.禁止手机睡眠 1 [UIApplication sharedApplication].idleTimerDisabled = YES; ...
- 1.Bootstrap-简介
1.介绍 Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的. 2.HTML 模板 一个使用了 Boots ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- Python日期时间Date/Time
Python程序可以处理多种方式的日期和时间.日期格式之间的转换是一种常见计算机的杂活. Python的时间和日历模块,能帮助处理日期和时间. Tick是什么? 为时间间隔,以秒为单位的浮点数.从“新 ...
- 小程序解释HTML富文本的两种办法
今天写着着代码,读取数据库的内容时突然跳出"<span>.<p>. "这些HTML标签.字符,吓一跳:本来如果是写HTML.JS倒也没什么,但是我在写小程序 ...
- Oracle 定时查询数据插入新表中(job+存储过程)
create table EGMAS_COUNT_DATA(TIMES date not null, COUNT NUMBER(30) not null, SYSTEM_NAME VARC ...
- Winform退出运行后,删除运行目录(批处理方法)
/// <summary> /// Winform程序退出删除运行目录 FormClosed调用 /// </summary> private void DeletExeFil ...
- Echarts数据可视化series-pie饼图,开发全解+完美注释
全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...