HDU 5818:Joint Stacks(stack + deque)
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.
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的时间来排序。看下样例就明白了。
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <stack> 5 #include <deque> 6 using namespace std; 7 #define N 100010 8 struct node 9 { 10 int val, id; 11 node () {} 12 node(int val, int id) : val(val), id(id) {} 13 }; 14 deque <node> a, b; 15 stack <node> c; 16 /* 17 官方题解: 18 比较简单巧妙的一个做法是引入一个新的栈C, 19 每次合并的时候就把A和B合并到C上,然后把A和B都清空. 20 push还是按正常做,pop注意当遇到要pop的栈为空时, 21 因为题目保证不会对空栈进行pop操作, 22 所以这时应直接改为对C栈进行pop操作. 23 这样做因为保证每个元素最多只在一次合并中被处理到, 24 pop和push操作当然也是每个元素只做一次,所以总复杂度是O(N)的. 25 26 因为在把A和B放到C上的时候要按照时间顺序放置,所以我就只会 27 搞一个deque,放到C里面的时候比较A和B队头的时间,然后小的先进栈C 28 其他的就和栈是一样的。 29 */ 30 31 int main() 32 { 33 int cas = 0; 34 int q; 35 while(scanf("%d", &q), q) { 36 printf("Case #%d:\n", ++cas); 37 while(!a.empty()) a.pop_back(); 38 while(!b.empty()) b.pop_back(); 39 while(!c.empty()) c.pop(); 40 int cnt = 0; 41 char s[6], ch, chh; 42 node n1, n2; 43 int dhh; 44 while(q--) { 45 scanf("%s %c", s, &ch); 46 if(s[1] == 'u') { 47 scanf("%d", &dhh); 48 if(ch == 'A') a.push_back(node(dhh, ++cnt)); 49 else b.push_back(node(dhh, ++cnt)); 50 } else if(s[1] == 'o') { 51 node ans; 52 if(ch == 'A') { 53 if(!a.empty()) { 54 ans = a.back(); 55 a.pop_back(); 56 } else { 57 ans = c.top(); 58 c.pop(); 59 } 60 } else { 61 if(!b.empty()) { 62 ans = b.back(); 63 b.pop_back(); 64 } else { 65 ans = c.top(); 66 c.pop(); 67 } 68 } 69 printf("%d\n", ans.val); 70 } else { 71 scanf("%*c%c", &chh); 72 while(1){ 73 if(a.empty() && b.empty()) break; 74 int t1 = 0x3f3f3f3f, t2 = 0x3f3f3f3f; 75 if(!a.empty()) { 76 n1 = a.front(); 77 t1 = n1.id; 78 } 79 if(!b.empty()) { 80 n2 = b.front(); 81 t2 = n2.id; 82 } 83 if(t1 < t2) { 84 c.push(n1); 85 a.pop_front(); 86 } else { 87 c.push(n2); 88 b.pop_front(); 89 } 90 } 91 } 92 } 93 } 94 return 0; 95 }