UVA 11995 I Can Guess the Data Structure!

开始按照训练指南上的习题来训练数据结构了。

这道题给出一系列操作,1 x表示加入x,2表示删除元素,根据这些操作来判断是哪个数据结构。

三种数据结构:栈(后进先出),队列(先进先出),优先队列(题目要求是最大堆)

需要注意删除操作,没有元素就不要再pop了。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
///OTHER
#define PB(x) push_back(x)
#define SZ() size()
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 100000000;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 1111;
const int M = 11;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

int s[N];
queue<int> q;
priority_queue<int> pq;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n, a, b, c, t;
    bool fs, fq, fpq;
    while(RI(n) != EOF)
    {
        t = 0;
        fs = fq = fpq = 1;
        while(!q.empty())q.pop();
        while(!pq.empty())pq.pop();
        REP(i, n)
        {
            RII(a, b);
            if(a == 1)
            {
                if(fs)s[t++] = b;
                if(fq)q.push(b);
                if(fpq)pq.push(b);
            }
            else
            {
                if(fs)
                {
                    if(t)
                    {
                        c = s[--t];
                        if(c != b)fs = 0;
                    }
                    else fs = 0;
                }
                if(fq)
                {
                    if(!q.empty())
                    {
                        c = q.front();
                        q.pop();
                        if(c != b)fq = 0;
                    }
                    else fq = 0;
                }
                if(fpq)
                {
                    if(!pq.empty())
                    {
                        c = pq.top();
                        pq.pop();
                        if(c != b)fpq = 0;
                    }
                    else fpq = 0;
                }
            }
        }
        c = fs + fq + fpq;
        if(!c)puts("impossible");
        else if(c == 1)
        {
            if(fs)puts("stack");
            if(fq)puts("queue");
            if(fpq)puts("priority queue");
        }
        else puts("not sure");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值