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 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.


栈是一种数据结构,仅允许在其中一端进行插入和删除,这一端被称为栈顶。最后入栈的元素将会最先出栈。换句话说,这个操作就是先进先出(LIFO)。

一个可合并的栈拥有"merge"操作。有如下三种操作:

- push A x: 将 x 插入栈 A

- pop A: 弹出 A 的栈顶元素

- merge A B: 合并栈 A 与 B

执行"merge A B"操作后,栈A将获得B中全部元素,随后B则为空。新栈中的元素根据先前的入栈时间重新排列,如同再执行他们的"push"操作到一个栈。参考输入/输出样例的详细说明。

给定两个可合并栈A与B,执行上述操作。


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.


多组测试用例。对于每个测试用例,第一行有一个整数N(0<N≤105),表示操作的数量。随后N行,每行包含一个"push"、 "pop"或"merge"指令。栈中的元素都是32位整数。保证"pop"操作不会出现在空栈中。N = 0时输入结束。


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.


对于每个用例,输出一行"Case #t:",t表示用例编号(从1开始)。对于每个"pop"操作,输出出栈的元素在单独一行。


Sample Input - 输入样例


Sample Output - 输出样例


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


Case #1:
2
1
Case #2:
1
2
3
0
Case #3:
1
2
3
0

【题解】

使用三个优先队列,一个给A,一个B,一个给合并的部分。

当A/B为空的时候,就从合并的部分取。

只用AB的话,感觉就不靠谱,懒癌发作,不想试。

【代码 C++】

 1 #include <cstdio>
 2 #include <queue>
 3 #define mx 100005
 4 int data[mx], id;
 5 int main(){
 6     int t = 1, n, x;
 7     char op[10], o;
 8     while (scanf("%d", &n), n){
 9         printf("Case #%d:\n", t++); id = -1;
10         std::priority_queue<int, std::vector<int> > a, b, m;
11
12         while (n--){
13             scanf("%s", op); getchar();
14             if (op[1] == ‘u‘){
15                 scanf("%c%d", &o, &data[++id]);
16                 if (o == ‘A‘) a.push(id);
17                 else b.push(id);
18             }
19             else if (op[1] == ‘o‘){
20                 if (getchar() == ‘A‘){
21                     if (a.empty()) x = data[m.top()], m.pop();
22                     else x = data[a.top()], a.pop();
23                 }
24                 else{
25                     if (b.empty()) x = data[m.top()], m.pop();
26                     else x = data[b.top()], b.pop();
27                 }
28                 printf("%d\n", x);
29             }
30             else{
31                 for (gets(op); !a.empty(); a.pop()) m.push(a.top());
32                 while (!b.empty()) m.push(b.top()), b.pop();
33             }
34         }
35     }
36     return 0;
37 }
时间: 2024-12-19 20:53:57

HDU 5818 Joint Stacks(联合栈)的相关文章

HDU 5818 Joint Stacks ——(栈的操作模拟,优先队列)

题意:有两个栈A和B,有3种操作:push,pop,merge.前两种都是栈的操作,最后一种表示的是如果“merge A B”,那么把B中的元素全部放到A中,且满足先入后出的栈原则. 分析:显然,我们给每一个节点配备一个时间戳即可.我一开始的思路是直接开两个优先队列进行直接模拟,merge操作就是把一个栈的元素全部倾倒到另一个栈内,但是会出现的问题是,如果有一个状态A和B的元素都相当多了,并且反复的进行merge操作,那么每一次merge都意味着大量的元素进出,肯定会超时的.因此,我们需要优化,

HDU 5818 Joint Stacks

搞了第三个栈来表示合并之后的.偷懒写了一个优先队列. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #inc

HDU 5818 Joint Stacks(左偏树)

题目链接:点击打开链接 思路: 该题的关键是怎么把两个栈合并, 我们可以使用一种叫左偏树的数据结构, 满足堆的性质和集合的性质,支持在O(logn)的复杂度下进行删除堆顶元素, 插入一个元素,合并两个堆. 细节参见代码: #include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; const int maxn = 152400; P v[maxn]; int tot, u, a, b, l[maxn

HDU - 5818 Joint Stacks 想法题 关键

http://codeforces.com/problemset/problem/669/D 题意:n个数1~N围成一个圈.q个操作包括操作1:输入x, 所有数右移x.操作2:1,2位置上的数(swap(a[1], a[2])交换:3,4交换.... 题解:观察,发现所有奇数行为都是一样的,偶数同理,(对于操作1 两者相同,对于操作2 奇数位++,偶数位上--: 模拟1,2,即可,然后依次输出其它数字即可.(看清 ac代码: #define _CRT_SECURE_NO_WARNINGS #in

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

2016暑假多校联合---Joint Stacks (STL)

HDU  5818 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 wor

暑假练习赛 004 E Joint Stacks

Joint StacksCrawling in process... Crawling failed Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5818 uDebug Description Input Output Sample Input Sample Output Hint Description A stack is a d

hdu-5818 Joint Stacks(模拟)

题目链接: Joint Stacks Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) 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

多校7 HDU5818 Joint Stacks

1 多校7 HDU5818 Joint Stacks 2 题意:n次操作.模拟栈的操作,合并的以后,每个栈里的元素以入栈顺序排列 3 思路:开三个栈,并且用到了merge函数 4 O(n)的复杂度 5 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define LL long long 9 const int inf = 0x3f3f3f3f; 10 const int MOD =998244353; 11 const int N =