hdu 5810 优先队列



链接:戳这里


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


题意:

n个操作

push A v :表示栈A中插入一个数v

push B v :表示栈B中插入一个数v

pop A :表示栈A删除头节点,并输出删除的数值

pop B :表示栈B删除头节点,并输出删除的数值

merge A B :表示栈B的元素按总的插入时间顺序放入栈A

merge B A :表示栈A的元素按总的插入时间顺序放入栈B


思路:

合并操作看作新开一个栈C,题目保证pop之前栈里面肯定有元素,所以pop操作当前如果A为空,则肯定是merge过的,输出C中的头节点


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n;
struct node{
    int v,id;
    node(int v=0,int id=0):v(v),id(id){}
    bool operator < (const node &a) const{
        return id<a.id;
    }
};
priority_queue<node> qu1;
priority_queue<node> qu2;
priority_queue<node> qu3;
char s[10],c[10],d[10];
int main(){
    int cas=0;
    while(scanf("%d",&n)!=EOF){
        if(n==0) break;
        while(!qu1.empty()) qu1.pop();
        while(!qu2.empty()) qu2.pop();
        while(!qu3.empty()) qu3.pop();
        printf("Case #%d:\n",++cas);
        for(int i=1;i<=n;i++){
            getchar();
            scanf("%s",s);
            if(strcmp(s,"push")==0){
                int v;
                scanf("%s%d",c,&v);
                if(c[0]=='A') qu1.push(node(v,i));
                else qu2.push(node(v,i));
            } else if(strcmp(s,"pop")==0){
                scanf("%s",c);
                if(c[0]=='A'){
                    if(qu1.size()>0){
                        node now=qu1.top();
                        qu1.pop();
                        printf("%d\n",now.v);
                    } else {
                        node now=qu3.top();
                        qu3.pop();
                        printf("%d\n",now.v);
                    }
                } else {
                    if(qu2.size()>0){
                        node now=qu2.top();
                        qu2.pop();
                        printf("%d\n",now.v);
                    } else {
                        node now=qu3.top();
                        qu3.pop();
                        printf("%d\n",now.v);
                    }
                }
            } else {
                scanf("%s%s",c,d);
                while(!qu1.empty()){
                    node now=qu1.top();
                    qu1.pop();
                    qu3.push(now);
                }
                while(!qu2.empty()){
                    node now=qu2.top();
                    qu2.pop();
                    qu3.push(now);
                }
            }
        }
    }
    return 0;
}


push A v :表示栈A中插入一个数v
pop A :表示栈A删除头节点,并输出删除的数值
merge A B :表示栈B的元素按总的插入时间顺序放入栈A
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值