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 (1≤n≤10001≤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 1 Sample 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 = , isq = , ispq = ;
while(n--) {
cin >> a >> x;
if(a == ) {
s.push(x);
q.push(x);
pq.push(x);
} else {
if(s.empty() || s.top() != x) iss = ;
if(q.empty() || q.front() != x) isq = ;
if(pq.empty() || pq.top() != x) ispq = ;
if(!s.empty()) s.pop();
if(!q.empty()) q.pop();
if(!pq.empty()) pq.pop();
}
}
if(iss + isq + ispq > )
puts("not sure");
else if(iss)
puts("stack");
else if(isq)
puts("queue");
else if(ispq)
puts("priority queue");
else
puts("impossible");
}
}

Kattis -I Can Guess the Data Structure!的更多相关文章

  1. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  2. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  4. Finger Trees: A Simple General-purpose Data Structure

    http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...

  5. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

  6. ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  7. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

随机推荐

  1. Spring cloud父项目的建立

    1.建立一个maven项目 注意建立项目的时候.选择pom的包 2.添加架包 <project xmlns="http://maven.apache.org/POM/4.0.0&quo ...

  2. Kattis - Game Rank

    Game Rank Picture by Gonkasth on DeviantArt, cc by-nd The gaming company Sandstorm is developing an ...

  3. Oracle下rman备份和还原到数据库任意一个时间点

    Rman备份为物理备份,启用rman备份必须开启数据库归档,开启归档后相当于给数据库加了一层双保险.Rman备份主要备份数据库的数据文件,控制文件,归档日志. RMAN 备份 一. 检查数据库是否启用 ...

  4. 功分器 power divider

    之前讲过有了解过耦合器,知道耦合器是矢网测量当中的关键设备 coupler //------------------------------------------------------------ ...

  5. with as递归调用

    一.递归调用--在代码中偶尔看到以记之,便于下次学习 https://blog.csdn.net/johnf_nash/article/details/78681060 --查询节点及其下所有子节点 ...

  6. CodeForces-546D Soldier and Number Game 筛法+动态规划

    题目链接:https://cn.vjudge.net/problem/CodeForces-546D 题意 抱歉,我给忘了,现在看题目又看不懂: P 思路 筛法+dp 话说这个函数应该是积性函数,然后 ...

  7. [tyvj-1061]Mobile Service 动态规划

    滚动数组优化一波. 原设计状态:表示三个员工分别的位置和执行到的任务. 考虑到:执行完第i个任务,一定有员工在pos[i],那么就可以压一维,空间复杂度就算不滚动数组也可以了. (喜闻乐见,今天第一次 ...

  8. redis helloworld

    一.启动 redis 服务 [root@MyLinux bin]# ./redis-server redis.conf 二.使用客户端连接服务 [root@MyLinux bin]# ./redis- ...

  9. Loaded APR based Apache Tomcat Native library 1.1.24 using APR version 1.4.6.

    Loaded APR based Apache Tomcat Native library 1.1.24 using APR version 1.4.6. 我复制的几个地方: MySql C:\WIN ...

  10. HDU 1788

    必须MARK下:任何时候都要保持清醒头脑,不要被题目绕了.. 其实就是求最小公倍数. #include <iostream> #include <cstdio> #includ ...