uva 11995 STL模拟

来源:互联网 发布:上淘宝开店要钱吗 编辑:程序博客网 时间:2024/05/05 02:40

记住 在pop之前 要先判断 是否 empty。runtime error 了 三次。


#include <iostream>#include <cstdio>#include <stack>#include <queue>using namespace std;stack<int> st;queue<int> qu;priority_queue<int> p_max;//priority_queue<int ,vector<int> , greater<int> > p_min;int flag_s,flag_q,flag_max;int flag_p,tmp,n;int opt,x,y;int main(){    freopen("1.in","r",stdin);    while (scanf("%d",&n)!=EOF){        while (!st.empty()) st.pop();        while (!qu.empty()) qu.pop();        while (!p_max.empty()) p_max.pop();        flag_max =flag_q =flag_s =1;        for (int i=0;i<n;i++) {            scanf("%d%d",&opt,&x);            if (opt==1) {                if (flag_s) st.push(x);                if (flag_q) qu.push(x);                if (flag_max) p_max.push(x);            }            else                if (st.empty() || qu.empty() || p_max.empty()) {                    flag_max =flag_q =flag_s =0;                }            else            {                if (flag_s) {                    if (st.empty()) {flag_s=0;}                    else{                    y=st.top();                    if (y!=x) flag_s=0;                    else  st.pop();                    }                }                if (flag_q) {                    y=qu.front();                    if (y!=x) flag_q=0;                    else if (!qu.empty()) qu.pop();                }                if (flag_max) {                    y=p_max.top();                    if (y!=x) flag_max=0;                    else if (!p_max.empty()) p_max.pop();                }            }        }        flag_p=flag_max;        tmp=flag_q+flag_s+flag_p;        if (tmp>1) printf("not sure\n");        else if (tmp==0) printf("impossible\n");             else {                if (flag_s) printf("stack\n");                if (flag_q) printf("queue\n");                if (flag_p) printf("priority queue\n");             }    }    return 0;}