• 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:
    ",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
    ",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;
    }
  • 相关阅读:
    《30天自制操作系统》06_day_学习笔记
    《30天自制操作系统》05_day_学习笔记
    《30天自制操作系统》04_day_学习笔记
    ugui Event.current.mousePosition获取的坐标原点在左上角
    场景中GameObject无法用代码隐藏问题(setActive为false)
    让camera实现类似cs第一人称视角旋转和位移
    itunesconnect如何提交被决绝过了的相同版本号
    mac下安装libpng环境
    golang实现模拟键盘按键
    cocos2d3.x在android下屏蔽多点触控
  • 原文地址:https://www.cnblogs.com/yu0111/p/5755317.html
Copyright © 2020-2023  润新知