HDU 5818 Joint Stacks

搞了第三个栈来表示合并之后的。偷懒写了一个优先队列。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\in.txt","r",stdin);
    freopen("D:\out.txt","w",stdout);
}
inline int read()
{
    char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0;
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar();  }
    return x;
}

const int maxn=100000+10;
struct X
{
    int f,num;
    X(int F,int Num)
    {
        f=F, num=Num;
    }
    bool operator < (const X &a) const {
        return f<a.f;
    }
};

priority_queue<X>Q;
stack<X>s[3];
int n,t;

int main()
{
    //File();
    int cas=1;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        printf("Case #%d:
",cas++);  t=1;
        while(!Q.empty()) Q.pop();
        while(!s[0].empty()) s[0].pop(); 
        while(!s[1].empty()) s[1].pop();
        for(int i=1; i<=n; i++)
        {
            char op[10];
            scanf("%s",op);
            if(strcmp("push",op)==0)
            {
                scanf("%s",op);
                if(op[0]=='A') { int x; scanf("%d",&x); s[0].push(X(t++,x)); }
                else { int x; scanf("%d",&x); s[1].push(X(t++,x)); }
            }
            else if(strcmp("pop",op)==0)
            {
                scanf("%s",op);
                if(op[0]=='A')
                {
                    if(!s[0].empty()) { printf("%d
",s[0].top().num); s[0].pop(); }
                    else { printf("%d
",Q.top().num); Q.pop(); }
                }
                else
                {
                    if(!s[1].empty()) { printf("%d
",s[1].top().num); s[1].pop(); }
                    else { printf("%d
",Q.top().num); Q.pop(); }
                }
            }
            else
            {
                char s1[5],s2[5]; scanf("%s%s",s1,s2);
                while(!s[0].empty()) { X h=s[0].top(); s[0].pop(); Q.push(h); }
                while(!s[1].empty()) { X h=s[1].top(); s[1].pop(); Q.push(h); }
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5759063.html