【题意】给出两个栈A B(初始时为空),有三种操作:push、pop、merge.其中merge是按照A B中元素进栈的相对顺序来重排的.

【解题方法】模拟水题,优先队列模拟即可。

【AC 代码】

#include <bits/stdc++.h>
using namespace std;
struct node{
    int v,ti;
    bool operator<(const node &rhs) const{
        return ti<rhs.ti;
    }
};
priority_queue<node>que1;
priority_queue<node>que2;
priority_queue<node>que3;

int main()
{
    int n;
    int cas=1;
    while(~scanf("%d",&n)&&n){
        printf("Case #%d:\n",cas++);
        while(!que1.empty()) que1.pop();
        while(!que2.empty()) que2.pop();
        while(!que3.empty()) que3.pop();
        int x,time=0;
        char op1[10],op2[10],op3[10];
        for(int i=1; i<=n; i++){
            scanf("%s",op1);
            if(strcmp(op1,"push")==0){
                scanf("%s%d",op2,&x);
                if(strcmp(op2,"A")==0){
                    que1.push(node{x,time++});
                }
                else{
                    que2.push(node{x,time++});
                }
            }
            else if(strcmp(op1,"pop")==0){
                scanf("%s",op2);
                if(strcmp(op2,"A")==0){
                    if(!que1.empty()){
                        int ans=que1.top().v;
                        que1.pop();
                        printf("%d\n",ans);
                    }
                    else{
                        int ans=que3.top().v;
                        que3.pop();
                        printf("%d\n",ans);
                    }
                }else{
                    if(!que2.empty()){
                        int ans=que2.top().v;
                        que2.pop();
                        printf("%d\n",ans);
                    }else{
                        int ans=que3.top().v;
                        que3.pop();
                        printf("%d\n",ans);
                    }
                }
            }else{
                scanf("%s%s",op2,op3);
                while(!que1.empty()){
                    node tmp=que1.top();
                    que1.pop();
                    que3.push(tmp);
                }
                while(!que2.empty()){
                    node tmp=que2.top();
                    que2.pop();
                    que3.push(tmp);
                }
            }
        }
    }
}