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.

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 }
时间: 2024-10-26 07:25:42

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 (Java/Others) Description 题目描述 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the "top" of

HDU 1009:FatMouse&#39; Trade(简单贪心)

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41982    Accepted Submission(s): 13962 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

hdu 5818 (优先队列) Joint Stacks

题目:这里 题意: 两个类似于栈的列表,栈a和栈b,n个操作,push a x表示把数x放进a栈的栈底,pop b 表示将栈b的栈顶元素取出输出,并释放这个栈顶元素,merge a b表示把后面的那个 栈里的元素全部压进前面的那个栈里面,并且压入后前面的栈的所有元素按其进栈顺序排列然后后面的栈变为了空. push和pop操作都还好,就是优先队列或者栈的操作,主要是merge操作,如果啊按照题目来模拟每次将后面的栈元素取出放入前面的栈,可能会超时,而超时的主要因素是每次都将 元素多的栈压入了元素少

HDU 4417:Super Mario(主席树)

http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意是:给出n个数和q个询问,每个询问有一个l,r,h,问在[l,r]这个区间里面有多少个数是小于等于h的. 思路:比较裸的主席树,注意题意给的区间是从[0,n-1],一开始看错导致想错了很多东西.询问的时候如果m < h,那么左子树全部都是小于 h 的,就加上左子树的 sum,继续查右子树,否则就查左子树.最后 l == r 的时候要判下 h >= l,因为这个也错了几次.从师兄那里学习到了如果找一

HDU 2089:不要62(数位DP)

http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众. 不吉利的数字为所有含有4或62的号码.例如: 62315 73418 88914 都属于不吉利号码.但是,61152虽然含有6和2,但

HDU - 6242:Geometry Problem(随机+几何)

Alice is interesting in computation geometry problem recently. She found a interesting problem and solved it easily. Now she will give this problem to you : You are given NN distinct points (Xi,Yi)(Xi,Yi) on the two-dimensional plane. Your task is to

HDU 4790:Just Random(容斥)

Just Random Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3932 Accepted Submission(s): 1276 Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game with n

HDU 2544:最短路( 最短路径入门 &amp;&amp;Dijkstra &amp;&amp; floyd )

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30972    Accepted Submission(s): 13345 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu