hdu 5818 Joint Stacks(栈的模拟)

官方题解:

Joint Stacks

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

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

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

另一种做法是用链表来直接模拟,复杂度也是O(N),但代码量稍大一些.

 

当时做题的时候用的是数组模拟,合并花费了大量的时间,so TLE!!!看了题解以后借助4个stack解决了

 

/*by*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
using namespace std;
typedef long long LL;
const LL N=100010;
const LL INF=0x3f3f3f3f;
typedef pair<LL,LL>p;
stack <p> a,b,c,d;
int main()
{
    int T,q=1;
    while(scanf("%d%*c",&T),T) {
        char s[50];
        LL tmp;
        printf("Case #%d:\n",q++);
        while(!a.empty()) a.pop();
        while(!b.empty()) b.pop();
        while(!c.empty()) c.pop();
        while(!d.empty()) d.pop();
        for(LL k=1; k<=T; k++) {
            scanf("%s%*c",s);
            if(strcmp(s,"push")==0) {
                scanf("%c %lld%*c",&s[20],&tmp);
                p tp;
                tp=make_pair(tmp,k);
                if(s[20]=='A') {
                    a.push(tp);
                } else {
                    b.push(tp);
                }
            } else if(strcmp(s,"pop")==0) {
                LL tm;
                scanf("%c%*c",&s[20]);
                if(s[20]=='A') {
                    if(!a.empty()) {
                        tm=a.top().first;
                        a.pop();
                    } else {
                        tm=d.top().first;
                        d.pop();
                    }
                } else {
                    if(!b.empty()) {
                        tm=b.top().first;
                        b.pop();
                    } else {
                        tm=d.top().first;
                        d.pop();
                    }
                }
                printf("%lld\n",tm);
            } else {
                scanf("%c %c%*c",&s[20],&s[21]);
                while(!a.empty()&&!b.empty()) {
                    p aa,bb;
                    aa=a.top();
                    bb=b.top();
                    if(aa.second>bb.second) {
                        c.push(aa);
                        a.pop();
                    } else {
                        c.push(bb);
                        b.pop();
                    }
                }
                while(!a.empty()) {
                    c.push(a.top());
                    a.pop();
                }
                while(!b.empty()) {
                    c.push(b.top());
                    b.pop();
                }
                while(!c.empty()) {
                    d.push(c.top());
                    c.pop();
                }

            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yu0111/p/5755317.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值