Joint Stacks---hdu5818(栈模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5818

 有3个操作pop,push,merge A B;

引入一个新的栈C,每次合并的时候就把A和B合并到C上,然后把A和B都清空. push还是按正常做,

pop时注意当遇到要pop的栈为空时,因为题目保证不会对空栈进行pop操作,所以这时应直接改为对C栈进行pop操作.

这样做因为保证每个元素最多只在一次合并中被处理到,pop和push操作当然也是每个元素只做一次,所以总复杂度是O(N)的;

 

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
using namespace std;
#define N 105
#define met(a, b) memset(a, b, sizeof(a))

typedef long long LL;

struct node
{
    int Id, num;
    node(){}
    node(int Id, int num) : Id(Id), num(num){}
};

int main()
{
    int n, tCase = 1;
    while(scanf("%d", &n) , n)
    {
        stack<node> a;
        stack<node> b;
        stack<node> c;
        stack<node> t;
        printf("Case #%d:\n", tCase++);
        for(int i=1; i<=n; i++)
        {
            char s1[15], s2[15];
            int num;
            scanf("%s", s1);
            if(strcmp(s1, "push") == 0)
            {
                scanf("%s %d", s2, &num);
                if(s2[0] == 'A')
                    a.push(node(i, num));
                else
                    b.push(node(i, num));
            }
            else if(strcmp(s1, "pop") == 0)
            {
                scanf("%s", s2);
                if(s2[0] == 'A')
                {
                    if(!a.empty())
                    {
                        node p = a.top();a.pop();
                        printf("%d\n", p.num);
                    }
                    else
                    {
                        node p = c.top();c.pop();
                        printf("%d\n", p.num);
                    }
                }
                else
                {
                    if(!b.empty())
                    {
                        node p = b.top();b.pop();
                        printf("%d\n", p.num);
                    }
                    else
                    {
                        node p = c.top();c.pop();
                        printf("%d\n", p.num);
                    }
                }
            }
            else
            {
                node p, q;
                scanf("%s %s", s1, s2);
                while(!a.empty() && !b.empty())
                {
                    p = a.top();
                    q = b.top();
                    if(p.Id > q.Id)
                    {
                        t.push(p);
                        a.pop();
                    }
                    else
                    {
                        t.push(q);
                        b.pop();
                    }
                }
                while(!a.empty())
                {
                    p = a.top();
                    a.pop();
                    t.push(p);
                }
                while(!b.empty())
                {
                    p = b.top();
                    b.pop();
                    t.push(p);
                }

                while(!t.empty())
                {
                    p = t.top();
                    t.pop();
                    c.push(p);
                }
            }
        }
    }
    return 0;
}
View Code

 

posted @ 2016-08-10 10:23  西瓜不懂柠檬的酸  Views(221)  Comments(0Edit  收藏  举报
levels of contents