HDU5818(多校第七场)——Joint Stacks(优先队列,左偏树)

Joint Stacks

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


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<N105) , 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


题意:就是两个堆,实现合并,推出堆顶元素,加入元素等操作。

两个堆合并的时,就是根据push的时间决定优先级。

我感觉就是裸的左偏树,因为左偏树能在常数时间内实现合并操作。加入是给每个点打上时间戳,把值对应到hash表里。

但也可以用两个优先队列优化。优化的方法就很多了。


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <cmath>
#include<queue>
using namespace std;
#define maxn 600100
#define INF 10000000007
#define pi cos(-1)
int haxibiao[maxn];
int v[maxn];
int l[maxn],r[maxn],d[maxn],tot;

int Merge(int x, int y)
{
    if (!x)
        return y;
    if (!y)
        return x;
    if (v[x]<v[y])
        swap(x,y);
    r[x]=Merge(r[x],y);
    if (d[l[x]]<d[r[x]])
        swap(l[x],r[x]);
    d[x]=d[r[x]]+1;
    return x;
}
int Init(int x)
{
    tot++;
    v[tot]=x;
    l[tot]=r[tot]=d[tot]=0;
    return tot;
}
int Insert(int x,int y)
{
    return (Merge(x,Init(y)));
}
int Top(int x)
{
    return v[x];
}
int pop(int x)
{
    return (Merge(l[x],r[x]));
}
int a,b;

int main()
{
    int n;
    int cas=1;
    while(scanf("%d",&n)&&n)
    {
        getchar();
        printf("Case #%d:\n",cas++);
        a=b=-3;
        tot=0;
        memset(haxibiao,0,sizeof(haxibiao));
        memset(v,0,sizeof(v));
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        memset(d,0,sizeof(d));
        char op[10];
        char name[2];
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",op);
            if(op[1]=='o')
            {
                scanf("%s",name);
                if(name[0]=='A')
                {
                    int s=Top(a);
                    printf("%d\n",haxibiao[s]);
                    a=pop(a);
                }
                else
                {
                    int s=Top(b);
                    printf("%d\n",haxibiao[s]);
                    b=pop(b);
                }
            }
            else if(op[1]=='u')
            {
                scanf("%s",name);
                int temp;
                scanf("%d",&temp);
                if(name[0]=='A')
                {

                    haxibiao[++cnt]=temp;
                    if (a==-3)
                        a=Init(cnt);
                    else
                        a=Insert(a,cnt);
                }
                else
                {
                    haxibiao[++cnt]=temp;
                    if (b==-3)
                        b=Init(cnt);
                    else
                        b=Insert(b,cnt);
                }
            }
            else if(op[1]=='e')
            {
                char a1[2],b1[2];
                scanf("%s%s",a1,b1);
                if(a1[0]=='A')
                {
                    if(a!=-3&&b!=-3)
                        a=Merge(a,b);
                    else if(a==-3&&b!=-3)
                        a=b;
                    b=-3;
                }
                else
                {
                    if(a!=-3&&b!=-3)
                        b=Merge(a,b);
                    else if(b==-3&&a!=-3)
                        b=a;
                    a=-3;
                }
            }
        }
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值