【天梯赛集训】7.21 栈和队列

目录

1-1 特殊堆栈

1-2 Windows消息队列

1-3 银行业务队列简单模拟

1-4 链表去重

1-5 出栈序列的合法性


1-1 特殊堆栈

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>

using namespace std;

stack<int> box; 
vector<int> up; //维护非递减序列

int main()
{
    int n;
    cin >> n;

    while(n--)
    {
        string s;
        cin >> s;

        if(s[1] == 'u')
        {
            int key;
            cin >> key;

            box.push(key);
            auto pos = upper_bound(up.begin(), up.end(), key);
            up.insert(pos, key);
        }
        else if(s[1] == 'o')
        {
            if(box.empty()) puts("Invalid");
            else
            {
                int key = box.top();
                box.pop();

                auto pos = lower_bound(up.begin(), up.end(), key);
                up.erase(pos);

                cout << key << endl;
            }
        }
        else
        {
            if(box.empty()) puts("Invalid");
            else
            {
                cout << up[(up.size() - 1) / 2] << endl;
            }
        }
    }

    return 0;
}

1-2 Windows消息队列

维护优先级

#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <string>

using namespace std;

priority_queue<int, vector<int>, greater<int>> q; //维护优先级的值
map<int, string> m;

int main()
{
    int n;
    cin >> n;

    while(n--)
    {
        string s;
        cin >> s;

        if(s[0] == 'P')
        {
            int key;
            cin >> s >> key;

            m[key] = s;
            q.push(key);
        }
        else
        {
            if(q.empty()) puts("EMPTY QUEUE!");
            else
            {
                int key = q.top();

                cout << m[key] << endl;

                q.pop();
            }
        }
    }

    return 0;
}

1-3 银行业务队列简单模拟

#include <bits/stdc++.h>

using namespace std;

queue<int> A, B;
vector<int> C;

int main()
{
    int n;
    cin >> n;

    while(n--)
    {
        int key;
        cin >> key;

        if(key % 2 == 0) B.push(key);
        else A.push(key);
    }

    for(int i = 1; A.size() && B.size(); i++) //用i模拟单位时间
    {
        if(A.size())
        {
            C.push_back(A.front());
            A.pop();
        }

        if(i % 2 == 0 && B.size())
        {
            C.push_back(B.front());
            B.pop();
        }
    }

    while(A.size())
    {
        C.push_back(A.front());
        A.pop();
    }

    while(B.size())
    {
        C.push_back(B.front());
        B.pop();
    }

    for(int i = 0; i < C.size(); i++)
    {
        cout << C[i];
        if(i != C.size() - 1) cout << ' ';
    }

    return 0;
}

1-4 链表去重

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 10;

int n;
int head, e[N], ne[N]; //模拟单链表
bool st[N];

vector<int> a, b, c;

int main()
{
    cin >> head >> n;

    memset(ne, -1, sizeof ne);

    for(int i = 0; i < n; i++)
    {
        int add, key, next;
        cin >> add >> key >> next;

        e[add] = key;
        ne[add] = next;
    }

    for(int i = head; i != -1; i = ne[i]) c.push_back(i); //依次存结点地址

    for(int i = 0; i < c.size(); i++)
    {
        int j = abs(e[c[i]]); //获取键值的绝对值
        if(!st[j])
        {
            st[j] = true;
            a.push_back(c[i]);
        }
        else
        {
            b.push_back(c[i]);
        }
    }

    printf("%05d %d ", a[0], e[a[0]]);
    for(int i = 1; i < a.size(); i++)
    {
        printf("%05d\n", a[i]);
        printf("%05d %d ", a[i], e[a[i]]);
    }
    printf("-1\n");

    if(b.size()) //注意测试点2:无需去重
    {
        printf("%05d %d ", b[0], e[b[0]]);
        for(int i = 1; i < b.size(); i++)
        {
            printf("%05d\n", b[i]);
            printf("%05d %d ", b[i], e[b[i]]);
        }
        printf("-1\n");
    }

    return 0;
}

1-5 出栈序列的合法性

#include <bits/stdc++.h>

using namespace std;

int a[1010], idx;

int main()
{
    int m, n, k;
    cin >> m >> n >> k;

    while(k--)
    {
        for(int i = 0; i < n; i++) cin >> a[i];

        idx = 1;
        stack<int> box;

        for(int i = 0; i < n; i++)
        {
            int x = a[i];

            while((box.empty() || box.top() != x) && box.size() < m && idx <= n)
                box.push(idx++);
            
            if(box.top() == x) box.pop();
            else
            {
                puts("NO");
                break;
            }

            if(i == n - 1) puts("YES");
        }
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花辞树dor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值