2016多校7 HDU 5818 Joint Stacks

2016多校联合训练#7

HDU 5818 Joint Stacks

优先队列

传送门:HDU


题意

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


思路

  • 三个优先队列,按照进入的时间顺序作为优先级。
  • 合并时清空A,B,全加入到C中。
  • 每次pop时,如果被pop的队列有元素,那么pop;没有就pop队列C。

代码

#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
#include <stack>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;

const int MAXN=200007;
const double eps=1e-8;
const int oo=2000000007;
struct ppp{
    int first,second;
    ppp (){};
    ppp (int _first,int _second)
    {
        first=_first;
        second=_second;
    }
    bool operator <(const ppp &a) const
    {
        return second<a.second;
    }
};
typedef struct ppp P;
int main()
{
    int test=1;
    int n;
    while(~scanf("%d",&n)&&n!=0)
    {
        printf("Case #%d:\n",test++);
        priority_queue<P> a,b,c;
        while(!a.empty()) a.pop();
        while(!b.empty()) b.pop();
        while(!c.empty()) c.pop();
        char p[10],q[10];
        int cnt=1;
        for(int i=0;i<n;i++)
        {
            scanf("%s%s",p,q);
            if(p[1]=='u')
            {
                int t;
                scanf("%d",&t);
                if(q[0]=='A') a.push(P(t,cnt));
                else b.push(P(t,cnt));
                cnt++;
            }
            else if(p[1]=='o')
            {
                if(q[0]=='A')
                {
                    if(a.empty())
                    {
                        printf("%d\n",c.top().first);
                        c.pop();
                    }
                    else
                    {
                        printf("%d\n",a.top().first);
                        a.pop();
                    }
                }
                else if(q[0]=='B')
                {
                    if(b.empty())
                    {
                        printf("%d\n",c.top().first);
                        c.pop();
                    }
                    else
                    {
                        printf("%d\n",b.top().first);
                        b.pop();
                    }
                }
            }
            else
            {
                char g[10];
                scanf("%s",g);
                while(!a.empty()&&!b.empty())
                {
                    if(a.top().second>b.top().second)
                    {
                        c.push(b.top());
                        b.pop();
                    }
                    else
                    {
                        c.push(a.top());
                        a.pop();
                    }
                }
                while(!a.empty())
                {
                    c.push(a.top());
                    a.pop();
                }
                while(!b.empty())
                {
                    c.push(b.top());
                    b.pop();
                }
            }
        }

    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值