hdu 5818 Joint Stacks (优先队列)

Joint Stacks

Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 341 Accepted Submission(s): 139

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 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,然后给你三种操作,对于每次 pop 操作输出.

思路:可以用三个优先队列来实现,也可以用三个栈来实现,原理相同,然后我们可以知道一个数最多被弹出来一次且不可能会有对空栈的操作,所以说,我们在合并的时候将所有的数都加到一个新的队列中,如果重新插入,那么还是插入 A 或者B,然后等到弹的时候判断所弹栈是否为空,如果为空则弹新栈中的数。

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-09-19.27 Tuesday
File Name    : D:\MyCode\2016-8月\2016-8-9-2.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
struct s
{
    int num,id;
    friend bool operator<(s A,s B)
    {
        return A.id<B.id;
    }
};
priority_queue<s>a;
priority_queue<s>b;
priority_queue<s>c;
int main()
{
    int n;int cas=0;
    while(scanf("%d",&n)!=EOF,n)
    {
        while(!a.empty()) a.pop();
        while(!b.empty()) b.pop();
        while(!c.empty()) c.pop();
        printf("Case #%d:\n",++cas);
        for(int i=1;i<=n;i++)
        {
            char str[10];
            scanf("%s",str);
            if(strcmp(str,"push")==0)
            {
                char ch[2];s x;
                scanf("%s%d",ch,&x.num);
                x.id=i;
                if(ch[0]=='A')
                    a.push(x);
                else
                    b.push(x);
            }
            else if(strcmp(str,"pop")==0)
            {
                char ch[2];scanf("%s",ch);s ans;
                if(ch[0]=='A')
                {
                    if(a.empty())
                    {
                        ans=c.top();
                        c.pop();
                    }
                    else
                    {
                        ans=a.top();
                        a.pop();
                    }
                }
                else
                {
                    if(b.empty())
                    {
                        ans=c.top();
                        c.pop();
                    }
                    else
                    {
                        ans=b.top();
                        b.pop();
                    }
                }
                printf("%d\n",ans.num);
            }
            else if(strcmp(str,"merge")==0)
            {
                char ch1[2],ch2[2];scanf("%s%s",ch1,ch2);
                while(!a.empty())
                {
                    c.push(a.top());
                    a.pop();
                }
                while(!b.empty())
                {
                    c.push(b.top());
                    b.pop();
                }
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值