HDU 5818:Joint Stacks(stack + deque)

时间:2022-02-25 13:05:46

http://acm.hdu.edu.cn/showproblem.php?pid=5818

Joint Stacks

Problem Description
 
A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of the stack. The last entry which is inserted is the first one that will be removed. In another word, the operations perform in a Last-In-First-Out (LIFO) manner.
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
 
There are multiple test cases. For each case, the first line contains an integer N(0<N≤105), 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
 
For each case, print a line "Case #t:", where t is the case number (starting from 1). For each "pop" operation, output the element that is popped, in a single line.
 
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
 
Sample Output
 
Case #1:
2
1
Case #2:
1
2
3
Case #3:
1
2
3
 
题意:有且仅有两个栈A和B,有三种操作,push、pop和merge,push和pop和普通的栈操作是一样的,merge的时候:merge X Y 相当于 把 Y 的元素合并入 X 里边,但是要根据该元素进栈X或者Y的时间来排序。看下样例就明白了。
 
 #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)的更多相关文章

  1. HDU 5818 Joint Stacks(联合栈)

    HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. 暑假练习赛 004 E Joint Stacks(优先队列模拟)

    Joint StacksCrawling in process... Crawling failed Time Limit:4000MS     Memory Limit:65536KB     64 ...

  3. HDU 2732:Leapin&&num;39&semi; Lizards(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:给出两个地图,蜥蜴从一个柱子跳跃到另外一个地方,那么这个柱子就可能会坍塌,第一个地图是柱子可以容忍跳 ...

  4. HDU 4055:Number String(DP计数)

    http://acm.hdu.edu.cn/showproblem.php?pid=4055 题意:给一个仅包含‘I','D','?'的字符串,’I'表示前面的数字比后面的数字要小(Increase升 ...

  5. hdu 5818 &lpar;优先队列&rpar; Joint Stacks

    题目:这里 题意: 两个类似于栈的列表,栈a和栈b,n个操作,push a x表示把数x放进a栈的栈底,pop b 表示将栈b的栈顶元素取出输出,并释放这个栈顶元素,merge a b表示把后面的那个 ...

  6. HDU 5898:odd-even number(数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5898 题意:给出一个区间[l, r],问其中数位中连续的奇数长度为偶数并且连续的偶数长度为奇数的个数.(1&lt ...

  7. HDU 4417:Super Mario(主席树)

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意是:给出n个数和q个询问,每个询问有一个l,r,h,问在[l,r]这个区间里面有多少个数是小于等于h的 ...

  8. HDU 1520:Anniversary party(树形DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description   There i ...

  9. HDU 6345:子串查询(前缀和)

    子串查询 Time Limit: 3500/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Sub ...

随机推荐

  1. UIButton 的属性与方法

    UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeCustom];//一般都是设置为该类型 btn.frame=CGRectMake(100, 80, ...

  2. Please see the &&num;39&semi;svn upgrade&&num;39&semi; command

    svn: E155036: Please see the 'svn upgrade' command svn: E155036: Working copy '/home/easwy/dev' is t ...

  3. Shell 语法之输入输出

    Linux 使用文件描述符标识每个文件对象.文件描述符是一个非负整数,可以唯一地标识会话中打开的文件.每个进程中最多可以有9个打开文件的描述符. Linux 标准文件描述符 文件描述符 缩写 描述 0 ...

  4. 【jqGrid for ASP&period;NET MVC Documentation】&period;学习笔记&period;4&period;性能

    1 HTML / ViewState 大小 1.1 HTML 大小 jqGrid for ASP.NET MVC 使用最佳的客户端渲染,意味着 HTML gird 的 尺寸是最小的.事实上,只有 gr ...

  5. 安装vs2013 Sqlserver 无法连接远程服务器的解决方法

    以“管理员身份”启动cmd,执行“netsh winsock reset”命令.

  6. Docker Win 10 安装

    最近了解了一下Docker,不看不知道,一了解就完全被它给吸引住了.以往要装个环境,除了要准备一个Linux系统,然后在安装各种版本的类库,再安装我们需要各种应用服务(如Redis,Ngix,Mong ...

  7. linux线程及互斥锁

    进程是资源管理的最小单元,线程是程序执行的最小单元.在操作系统的设计上,从进程演化出线程,最主要的目的就是更好的支持SMP以及减小(进程/线程)上下文切换开销. 就像进程有一个PID一样,每个线程也有 ...

  8. 谷歌浏览器运行Flash

    最近有人问我谷歌浏览器的flash总是要点击手动运行才可以使用.看了很多网上很多教程,并没有比较好的解决方案. 自己找了相关资料后,找到了一个比较好的完整的.特此在这边放出来给大家使用. 新建记事本, ...

  9. 梯度消失 &sol; 梯度爆炸以及Xavier初始化

    2018-12-06 16:25:08 首先我们先来看一下求解梯度的公式,以下面三层的网络为例: 如果w初始化为大于1的数字,在深层神经网络计算梯度的时候就会出现梯度爆炸的现象: 如果w初始化为小于1 ...

  10. 【Android】Sensor框架Framework层解读

    Sensor整体架构 整体架构说明 黄色部分表示硬件,它要挂在I2C总线上 红色部分表示驱动,驱动注册到Kernel的Input Subsystem上,然后通过Event Device把Sensor数 ...