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
0
 
Sample Output
 
Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0
 
题意:有且仅有两个栈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' 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 (优先队列) 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< ...

  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. 网页flv下载探索_1

    最近看了一个优酷视频(非优酷网站,最终地址指向优酷),用chrome开发者工具,可找到flv地址如下,简单摘录如下: http://27.221.100.104/657D4D2878C3382C781 ...

  2. 使用sh-x调试shell脚本_转

    参考:http://blog.chinaunix.net/uid-20564848-id-73502.html 1. 通过sh -x 脚本名  #显示脚本执行过程2.脚本里set -x选项,轻松跟踪调 ...

  3. iOS开发 - 网络数据安全加密(MD5)

    提交用户的隐私数据 一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 用户的隐私数据登 ...

  4. mysqld_multi部署mysql单机多实例

    1.安装gcc-c++.ncurses依赖包 # yum install gcc-c++ ncurses-devel 2.安装cmake,用来编译mysql # tar -xvf cmake-3.2. ...

  5. Java FX中TreeView节点选中和双击事件监听

    TreeItem<String> treeRoot = new TreeItem<String>("Root"); treeRoot.setExpanded ...

  6. .net开发,html ajax开发架构之我见 bs ajax最简化法 Knock out Request,totally oo

    .net开发中,无论ajax还是webform,webpage, 总免不了要和request这个静态全局,可以远程通信的对象打交道. 而对于软件来讲,按照Matin Fowler的的面向对象,可利用软 ...

  7. 给三个int,判断是否可构成三角形算法

    哎,今天跟同事讨论算法,有一个女生给我出了这样一个题目,bool Test(int a,int b,int c)感觉很简单,实际呢?自己考虑的不够全面.在得到了提示之后呢,也还是找不到很好的解决方案. ...

  8. SpringMvc:处理模型数据

    SpringMvc提供了以下途径输出模型数据: -ModelAndView:处理方法返回值类型为ModelAndView,方法体即可通过该对象添加模型数据 -Map或Model:入参为org.spri ...

  9. 关于LED 流水灯的软件调试方法(非开发板调试)

    因为: 硬件 norflash 有寿命,所以尽量少用,而且自己也不会把 程序在 KEIL中从SDRAM 中调试,不会设置.所以采取软件虚拟的方法调试. 主要修改一下几部分: 1.  ledcircle ...

  10. 供应商 银行 SQL (转自ITPUB)

    在此记录一下自己学习过程.新手,请多多指教,谢谢. 最近客户有需求,找出供应商对应的银行信息,查看了下网上帖子,发现都是从供应商及供应商地点层发起,去查找对应的银行信息,但是,供应商维护银行界面共有四 ...