码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5818 Joint Stacks

时间:2016-08-10 22:39:30      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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

#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:\n",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\n",s[0].top().num); s[0].pop(); }
                    else { printf("%d\n",Q.top().num); Q.pop(); }
                }
                else
                {
                    if(!s[1].empty()) { printf("%d\n",s[1].top().num); s[1].pop(); }
                    else { printf("%d\n",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;
}

 

HDU 5818 Joint Stacks

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/5759063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!