暑假练习赛 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,全都进去)进来,当最后所有人都到了还会再开一次门,让还没进来的人进来,每次都是礼物价值高的人先进.最后 ...
随机推荐
- 600集Python从入门到精通教程(懂中文就能学会)
目录大纲: 本套教程15天 1-3 天内容为Linux基础命令 4-13 天内容为Python基础教程 14-15 天内容为 飞机大战项目演练 视频概括: 第一阶段(1-3天): 该阶段首先通过 ...
- hdu4704 Sum 2013 Multi-University Training Contest 10 数论题
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...
- numpy学习整理
今天先整理到这里,剩下的下次再整理 1.改变形状: reshape()返回改变的数组形状,但无法改变源数组形状 resize() 可以改变源数组形状 ravel() 输出类似C数组的列表,和resha ...
- Python自学笔记-paramiko模块(Mr serven)
文章出处:http://www.cnblogs.com/wupeiqi/articles/5095821.html SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: #!/u ...
- Python自学笔记-进程,线程(Mr serven)
对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程,打开一个Word就启动了 ...
- 使用VLC创建组播流
vlc既是一个播放器,又可以成为一个流媒体服务器.最近需要做udp组播播放相关的东西,需要先在本地搭建一个udp组播服务器,因为机器上本来就装有vlc,所以就用它了. 第一步: 点击媒体->流 ...
- samba的安装和配置
samba是Linux系统上的一种文件共享协议,可以实现Windows系统访问Linux系统上的共享资源,现在介绍一下如何在Ubuntu 14.04上安装和配置samba 实验环境 Ubuntu 14 ...
- python 设计模式,“多”例模式
版本1:一个账号不能同时是司机乘客. #-*- coding:utf-8 -*- ''' Created on 2016年8月2日 @author: yangfanholiday ''' class ...
- marked插件在线实时解析markdown的web小工具
访问地址: https://mdrush.herokuapp.com/ github项目: https://github.com/qcer/MDRush 实现简介: 1.动态数据绑定 借助Vuejs, ...
- Linux目录结构详解(一)
Linux目录结构,在逻辑上所有目录只有一个顶点,即/(根目录),是所有目录的起点.根下面类似于一个倒挂着的树的结构. Linux目录按照类别组织: 应用程序 /usr/bin 数据文件,帮助/usr ...
uDebug