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 ...
随机推荐
- 随机数产生random
随机数产生推荐用random(),在产生随机数前要添加种子srandom((unsigned int)time(NULL)). SYNOPSIS #include <stdlib.h> l ...
- iOS开发 - 网络数据安全加密(MD5)
提交用户的隐私数据 一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 用户的隐私数据登 ...
- Oracle 11g RAC INS-06006 Passwordless SSH connectivity not set up between the following node(s)
安装11g RAC的grid时,在Test互信的时候报错INS-06006 Passwordless SSH connectivity not set up between the following ...
- jquery对strutrs2 <s:radio>标签的设置和取值
今天郁闷了1小时. 需求是这样的: <s:radio list="#{0:'男',1:'女'}" value="member.sex" id=" ...
- 自动备份sqlexpress 数据库脚本
Create PROCEDURE [dbo].[usp_BackupDatabase] @databaseName sysname,@backupPath nvarchar(255), @backup ...
- PostgreSQL trigger (function) examples
postgres=# \c warehouse_db You are now connected to database "warehouse_db" as user " ...
- Lintcode: Previous Permuation
Given a list of integers, which denote a permutation. Find the previous permutation in ascending ord ...
- Winform TreeView 节点拖动
private void treeView_ItemDrag(object sender, ItemDragEventArgs e) { TreeNode dragNode = e.Item as T ...
- csuoj 1337: 搞笑版费马大定理
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 1337: 搞笑版费马大定理 Time Limit: 1 Sec Memory Limit ...
- [转]JVM内幕:Java虚拟机详解
本文由 ImportNew - 挖坑的张师傅 翻译自 jamesdbloom.欢迎加入翻译小组.转载请见文末要求. 这篇文章解释了Java 虚拟机(JVM)的内部架构.下图显示了遵守Java SE 7 ...