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
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
struct Node{
int num;
int time;
Node(int a = 0 , int b = 0){
num=a,time=b;
}
};
bool operator<(Node a,Node b){
return a.time<b.time;
}
priority_queue<Node> A;
priority_queue<Node> B;
//struct cmp{
//
// bool operator()(Node a,Node b){
// return a.time<b.time;
// }
//};
//priority_queue<Node,vector<Node>,cmp> A;
//priority_queue<Node,vector<Node>,cmp> B;
void init(){
while(!A.empty()){
A.pop();
}
while(!B.empty()){
B.pop();
}
}
char opr[7];
char obj;
char obj2;
int i;
int main(){
int n;
int c=1;
while(~scanf("%d",&n)&&n){
int t=0;
init();
printf("Case #%d:\n",c++);
while(n--){
scanf("%s %c",opr,&obj);
if(opr[1]=='u'){
scanf("%d",&i);
if(obj=='A') A.push(Node(i,++t));
if(obj=='B') B.push(Node(i,++t));
}
else if(opr[1]=='o'){
if(obj=='A'){
printf("%d\n",A.top().num);
A.pop();
}
if(obj=='B'){
printf("%d\n",B.top().num);
B.pop();
}
}
else if(opr[1]=='e'){
getchar();
scanf("%c",&obj2);
if(obj=='A'&&obj2=='B'){
if(A.size()<B.size()) swap(A,B);
while(!B.empty()){
A.push(B.top());
B.pop();
}
}
if(obj=='B'&&obj2=='A'){
if(B.size()<A.size()) swap(A,B);
while(!A.empty()){
B.push(A.top());
A.pop();
}
}
}
}
}
}
题意:有A B两个栈,对A B进行push,pop,merge操作,push pop和栈操作中的进栈出栈一样,merge操作是将后一个栈融合到前一个栈中,融合时元素按进栈顺序排列;输出每次pop的元素;
题意给的是栈, 一开始可能会考虑用栈做;但是后面merge的时候不好操作,因为要按push的时间进栈,所以考虑用优先队列进行操作;
用结构体变量存储进栈时的数字和进栈时的时间,自定义优先队列的优先级为时间(时间短的在队末);
进队时用结构体构造一个变量进队,merge时不断让后一个队列的队首进入前一个队列,再把后一个队列的队首pop;
注意的是:譬如merge A B,当前A元素很少而B元素很多时,不断的push pop很容易超时,此时我们考虑将它们队内元素交换,这样可以节省时间;
交换时我们直接用algorithm库的swap函数:swap只交换内部的指针,时间复杂度为O(1);