首页 技术 正文
技术 2022年11月6日
0 收藏 817 点赞 211 浏览 2833 个字

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 4push A 1push A 2pop Apop A9push A 0push A 1push B 3pop Apush A 2merge A Bpop Apop Apop A9push A 0push A 1push B 3pop Apush A 2merge B Apop Bpop Bpop B 0 Sample Output Case #1: 2 1Case #2: 1230Case #3:1230 题意:有且仅有两个栈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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905