• Kattis -I Can Guess the Data Structure!


    I Can Guess the Data Structure!

    There is a bag-like data structure, supporting two operations:

    1 x1 x: Throw an element xx into the bag.

    22: Take out an element from the bag.

    Given a sequence of operations with return values, you’re going to guess the data structure. It is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!

    Input

    There are several test cases. Each test case begins with a line containing a single integer nn (1n10001≤n≤1000). Each of the next nn lines is either a type-1 command, or an integer 22 followed by an integer xx. This means that executing the type-2 command returned the element xx. The value of xx is always a positive integer not larger than 100100. The input is terminated by end-of-file (EOF). The size of input file does not exceed 1MB.

    Output

    For each test case, output one of the following:

    stack
    It’s definitely a stack.

    queue
    It’s definitely a queue.

    priority queue
    It’s definitely a priority queue.

    impossible
    It can’t be a stack, a queue or a priority queue.

    not sure
    It can be more than one of the three data structures mentioned above.

    Sample Input 1Sample Output 1
    6
    1 1
    1 2
    1 3
    2 1
    2 2
    2 3
    6
    1 1
    1 2
    1 3
    2 3
    2 2
    2 1
    2
    1 1
    2 2
    4
    1 2
    1 1
    2 1
    2 2
    7
    1 2
    1 5
    1 1
    1 3
    2 5
    1 4
    2 4
    1
    2 1
    
    queue
    not sure
    impossible
    stack
    priority queue
    impossible

    题意

    模拟栈stack,队列queue,和优先队列priority queue的进出,判断属于哪一个

    思路

    用stl模拟

    代码

    #include<bits/stdc++.h>
    using namespace std;
    int main() {
        int n, a, x;
        while(cin >> n) {
            stack<int> s;
            queue<int> q;
            priority_queue<int> pq;
            bool iss = 1, isq = 1, ispq = 1;
            while(n--) {
                cin >> a >> x;
                if(a == 1) {
                    s.push(x);
                    q.push(x);
                    pq.push(x);
                } else {
                    if(s.empty() || s.top() != x) iss = 0;
                    if(q.empty() || q.front() != x) isq = 0;
                    if(pq.empty() || pq.top() != x) ispq = 0;
                    if(!s.empty()) s.pop();
                    if(!q.empty()) q.pop();
                    if(!pq.empty()) pq.pop();
                }
            }
            if(iss + isq + ispq > 1)
                puts("not sure");
            else if(iss)
                puts("stack");
            else if(isq)
                puts("queue");
            else if(ispq)
                puts("priority queue");
            else
                puts("impossible");
        }
    }
  • 相关阅读:
    c# 查找进程
    第三方打包工具
    WebClient 上传和下载
    .net 读取文件
    winfrom 圆角panel
    窗体或控件的两种拖动方式
    winfrom 获取当前屏幕尺寸
    动态修改配置文件web服务地址
    将字符串编码成 GBK
    .net(C#) 读取配置文件
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6284135.html
Copyright © 2020-2023  润新知