UVa 11995

题意

一个包包, 可能是栈, 队列, 优先队列, 或其他
操作1代表存入数据
操作2代表拿出顶端/首端的一个元素

思路

本以为是水题, 居然还WA了两次
仔细思考 发现有个特殊情况没考虑到: 如果存入数量小于取出数, 那么一定是impossible !!!

AC代码

操作的时候要先判断容器是否非空吖
用完要清空容器吖

#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

stack<int> a;
queue<int> b;
priority_queue<int> c;

int main()
{
    int T, x, y;
    int fa,fb,fc;
    int one, two;
    while( ~scanf("%d",&T) )
    {
        fa = 1, fb = 1, fc = 1;
        one = 0, two = 0;
        while(T--){
            scanf("%d%d",&x,&y);
            if( x == 1 ){
                one++;
                a.push(y);
                b.push(y);
                c.push(y);
            }
            else{
                two++;
                if( !a.empty() ){
                    if( a.top() != y )  fa = 0;
                    else a.pop();
                }
                if( !b.empty() ){
                    if( b.front() != y )  fb = 0;
                    else b.pop();
                }
                if( !c.empty() ){
                    if( c.top() != y )  fc = 0;
                    else c.pop();
                }
            }
        }
        if( !fa && !fb && !fc || (two > one) )   puts("impossible");
        else if( fa + fb + fc > 1 )      puts("not sure");
        else if( fa )      puts("stack");
        else if( fb )      puts("queue");
        else               puts("priority queue");
        while( !a.empty() )     a.pop();
        while( !b.empty() )    b.pop();
        while( !c.empty() )    c.pop();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/JinxiSui/p/9740625.html