zoukankan      html  css  js  c++  java
  • uva11995 I Can Guess the Data Structure!

    就是用STL来模拟就行了,但是我有一个地方没注意坑了好几次:访问STL封装好的数据结构中的元素之前先判断容器是不是为空,否则会Runtime Error。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<cctype>
    #include<sstream>
    using namespace std;
    #define pii pair<int,int>
    #define LL long long int
    const double eps=1e-10;
    const int INF=1000000000;
    const int maxn=1000+10;
    int n,a[maxn],b[maxn];
    int yes[3];
    stack<int> st;
    queue<int> que;
    priority_queue<int> pq;
    void ini()
    {
        while(!st.empty()) st.pop();
        while(!que.empty()) que.pop();
        while(!pq.empty()) pq.pop();
        yes[0]=1;yes[1]=1;yes[2]=1;
    }
    int main()
    {
        //freopen("in1.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        while(scanf("%d",&n)==1)
        {
            ini();
            for(int i=0;i<n;i++)
            {
                scanf("%d%d",&a[i],&b[i]);
            }
            for(int i=0;i<n;i++)
            {
                if(a[i]==1)
                {
                    if(yes[0]==1) st.push(b[i]);
                    if(yes[1]==1) que.push(b[i]);
                    if(yes[2]==1) pq.push(b[i]);
                }
                else
                {
                    if(yes[0]==1)
                    {
                        if(!st.empty()&&st.top()==b[i]) st.pop();
                        else yes[0]=0;
                    }
                    if(yes[1]==1)
                    {
                        if(!que.empty()&&que.front()==b[i]) que.pop();
                        else yes[1]=0;
                    }
                    if(yes[2]==1)
                    {
                        if(!pq.empty()&&pq.top()==b[i]) pq.pop();
                        else yes[2]=0;
                    }
                }
            }
            int num=0;
            for(int i=0;i<3;i++)
            {
                if(yes[i]==1)
                    num++;
            }
            if(num==0) puts("impossible");
            else if(num>=2) puts("not sure");
            else
            {
                if(yes[0]==1) puts("stack");
                else if(yes[1]==1) puts("queue");
                else puts("priority queue");
            }
        }
        return 0;
    }
  • 相关阅读:
    Attributes in C#
    asp.net C# 时间格式大全
    UVA 10518 How Many Calls?
    UVA 10303 How Many Trees?
    UVA 991 Safe Salutations
    UVA 10862 Connect the Cable Wires
    UVA 10417 Gift Exchanging
    UVA 10229 Modular Fibonacci
    UVA 10079 Pizza Cutting
    UVA 10334 Ray Through Glasses
  • 原文地址:https://www.cnblogs.com/zywscq/p/4279761.html
Copyright © 2011-2022 走看看