暑假练习赛 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,全都进去)进来,当最后所有人都到了还会再开一次门,让还没进来的人进来,每次都是礼物价值高的人先进.最后 ...
随机推荐
- angular之表单验证与ngMessages
刚接触angular1.x很多经常用到的ngMessages的地方,这里顺便记一下,效果如下图: 如果引用了angular-messages.js报如下错误,说明你的angular.js和angula ...
- GCD之信号量机制一
在使用NSOperationQueue进行多线程编程时,可通过[queue setMaxConcurrentOperationCount:5]来设置线程池中最多并行的线程数,在GCD中信号量机制也和它 ...
- poj2155一个二维树状数组
...
- Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- CSS图片垂直居中方法整理集合
原帖链接:http://bbs.blueidea.com/thread-2666987-1-1.html 1.因为Opera,FF3,IE8均支持display:talbe;这些特性了,因此改进的办法 ...
- 实现径向变换用于样本增强《Training Neural Networks with Very Little Data-A Draft》
背景: 做大规模机器学习算法,特别是神经网络最怕什么--没有数据!!没有数据意味着,机器学不会,人工不智能!通常使用样本增强来扩充数据一直都是解决这个问题的一个好方法. 最近的一篇论文<Trai ...
- Centos7安装Percona5.7
OS: Centos7.0 DB: Percona5.7 1. 通过yum安装 ## 删除之前的mysql数据库, 我用的是centos7.再安装虚拟机的时候,预装了很多软件.所以mysql和mari ...
- HDU1068 二分匹配 独立集
前边和后边性别不同!!!不然NP了 Girls and Boys Problem Description the second year of the university somebody star ...
- Linux-fdisk磁盘分区命令(16)
名称: fdisk 使用: fdisk [块设备磁盘] 说明: 将一个块设备(磁盘)分成若干个块设备(磁盘),并将分区的信息写进分区表. fdisk命令菜单常用参数如下所示: d:(del)删除一个 ...
- MySQL(十五)之数据备份中mysqldump详解
前言 其实前面一篇数据备份已经是非常的详细了,这里我想单独的讲解一下mysqldump,相信很多程序员都是用过这个命令的! 一.MySQL数据库的备份与还原 1.1.MySQL数据库备份 1)语法 m ...