HDU 5818:Joint Stacks(stack + deque)
http://acm.hdu.edu.cn/showproblem.php?pid=5818
Joint Stacks
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.
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <deque>
using namespace std;
#define N 100010
struct node
{
int val, id;
node () {}
node(int val, int id) : val(val), id(id) {}
};
deque <node> a, b;
stack <node> c;
/*
官方题解:
比较简单巧妙的一个做法是引入一个新的栈C,
每次合并的时候就把A和B合并到C上,然后把A和B都清空.
push还是按正常做,pop注意当遇到要pop的栈为空时,
因为题目保证不会对空栈进行pop操作,
所以这时应直接改为对C栈进行pop操作.
这样做因为保证每个元素最多只在一次合并中被处理到,
pop和push操作当然也是每个元素只做一次,所以总复杂度是O(N)的. 因为在把A和B放到C上的时候要按照时间顺序放置,所以我就只会
搞一个deque,放到C里面的时候比较A和B队头的时间,然后小的先进栈C
其他的就和栈是一样的。
*/ int main()
{
int cas = ;
int q;
while(scanf("%d", &q), q) {
printf("Case #%d:\n", ++cas);
while(!a.empty()) a.pop_back();
while(!b.empty()) b.pop_back();
while(!c.empty()) c.pop();
int cnt = ;
char s[], ch, chh;
node n1, n2;
int dhh;
while(q--) {
scanf("%s %c", s, &ch);
if(s[] == 'u') {
scanf("%d", &dhh);
if(ch == 'A') a.push_back(node(dhh, ++cnt));
else b.push_back(node(dhh, ++cnt));
} else if(s[] == 'o') {
node ans;
if(ch == 'A') {
if(!a.empty()) {
ans = a.back();
a.pop_back();
} else {
ans = c.top();
c.pop();
}
} else {
if(!b.empty()) {
ans = b.back();
b.pop_back();
} else {
ans = c.top();
c.pop();
}
}
printf("%d\n", ans.val);
} else {
scanf("%*c%c", &chh);
while(){
if(a.empty() && b.empty()) break;
int t1 = 0x3f3f3f3f, t2 = 0x3f3f3f3f;
if(!a.empty()) {
n1 = a.front();
t1 = n1.id;
}
if(!b.empty()) {
n2 = b.front();
t2 = n2.id;
}
if(t1 < t2) {
c.push(n1);
a.pop_front();
} else {
c.push(n2);
b.pop_front();
}
}
}
}
}
return ;
}
HDU 5818:Joint Stacks(stack + deque)的更多相关文章
- HDU 5818 Joint Stacks(联合栈)
HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- 暑假练习赛 004 E Joint Stacks(优先队列模拟)
Joint StacksCrawling in process... Crawling failed Time Limit:4000MS Memory Limit:65536KB 64 ...
- HDU 2732:Leapin' Lizards(最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:给出两个地图,蜥蜴从一个柱子跳跃到另外一个地方,那么这个柱子就可能会坍塌,第一个地图是柱子可以容忍跳 ...
- HDU 4055:Number String(DP计数)
http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意:给一个仅包含‘I','D','?'的字符串,’I'表示前面的数字比后面的数字要小(Increase升 ...
- hdu 5818 (优先队列) Joint Stacks
题目:这里 题意: 两个类似于栈的列表,栈a和栈b,n个操作,push a x表示把数x放进a栈的栈底,pop b 表示将栈b的栈顶元素取出输出,并释放这个栈顶元素,merge a b表示把后面的那个 ...
- HDU 5898:odd-even number(数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=5898 题意:给出一个区间[l, r],问其中数位中连续的奇数长度为偶数并且连续的偶数长度为奇数的个数.(1< ...
- HDU 4417:Super Mario(主席树)
http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意是:给出n个数和q个询问,每个询问有一个l,r,h,问在[l,r]这个区间里面有多少个数是小于等于h的 ...
- HDU 1520:Anniversary party(树形DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description There i ...
- HDU 6345:子串查询(前缀和)
子串查询 Time Limit: 3500/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Sub ...
随机推荐
- Java Synchronized的用法
synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: 1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个代码 ...
- 第一篇 SQL Server代理概述
本篇文章是SQL Server代理系列的第一篇,详细内容请参考原文. SQL Server代理是SQL Server的作业调度和告警服务,如果使用得当,它可以大大简化DBA的工作量.SQL Serve ...
- bootstrap学习之一_bootstrap css
一.文本相关样式 文本格式(用于块状标签):text-left:向左对齐文本:text-center:居中对齐文本:text-right:向右对齐文本:text-justified;text-nowr ...
- 重写NSLog,Debug模式下打印日志和当前行数
在pch文件中加入以下命令,NSLog在真机测试中就不会打印了 //重写NSLog,Debug模式下打印日志和当前行数 #if DEBUG #define NSLog(FORMAT, ...) fpr ...
- ORACLE数据库用户账号处于expired状态如何处理
账户过期,必须要用户更改密码, 账户才能重新使用. 但有些时候, 因为各种原因, 我们并不知道原密码的明文是什么,但很多时候又不能修改已有密码,好在可以用原密码来更改密码. 在11G中,dba_use ...
- jetbrains公司全系列软件最简单破解教程
由于最新jetbrains发布了IntelliJ IDEA 15. PyCharm 5.PhpStorm10.WebStorm 11.RubyMine等各个版本,但是改变了注册方法.原先的注册码包括注 ...
- Eclipse下配置C++开发环境(转)
1. 首先确保你的电脑上已经安装了Java,如果没有,或者不确定,请到官网上下载并安装,网址如下(这一步我就不详述了): http://www.java.com/zh_CN/ 2. 到官网上下载并 ...
- C++之路进阶——codevs2366(朋友圈)
2366 朋友圈 2012年省队选拔赛河北 时间限制: 10 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 在很久很久以前,曾经 ...
- 变形--扭曲 skew()
变形--扭曲 skew() 扭曲skew()函数能够让元素倾斜显示.它可以将一个对象以其中心位置围绕着X轴和Y轴按照一定的角度倾斜.这与rotate()函数的旋转不同,rotate()函数只是旋转,而 ...
- 。。。HibernateTemplate与Session。。。
今天在学习Spring框架的时候,突然发现了这个类----HibernateTemplate,这个类与Session一开始认为是差不多的,这个HibernateTemplate类对象拥有Session ...