11995 - I Can Guess the Data Structure!

Time limit: 1.000 seconds

Problem I

I Can Guess the Data Structure!

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

1 x

Throw an element x into the bag.

2

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 n (1<=n<=1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get an element x without error. The value of x is always a positive integer not larger than 100. 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

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

Output for the Sample Input

queue
not sure
impossible
stack
priority queue

Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!

题解:STL中 queue、stack、priority_queue的使用。

代码:

 #include<cstdio>
#include<cstring>
#include<stdbool.h>
#include<cmath>
#include<queue>
#include<stack>
#include<algorithm> #define rep(i,a,b) for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) ((x)*(x)) using namespace std; int i,j,x,y,pri,qui,sti; void check()
{
if(qui== && pri== && sti==) printf("queue\n");
else
if(qui== && pri== && sti==) printf("priority queue\n");
else
if(qui== && pri== && sti==) printf("stack\n");
else
if(qui== && pri== && sti==) printf("impossible\n");
else
printf("not sure\n");
} int main()
{
int T; while(scanf("%d",&T)!=EOF) {
pri=qui=sti=; queue <int> Q;
stack <int> S;
priority_queue <int> prQ; while(T--) {
scanf("%d%d",&x,&y);
if(x==) {
Q.push(y);
S.push(y);
prQ.push(y);
}
else { if(qui==) {
if(Q.empty() || Q.front()!=y) qui=;
if(!Q.empty()) Q.pop();
} if(sti==) {
if(S.empty() || S.top()!=y) sti=;
if(!S.empty()) S.pop();
} if(pri==) {
if(prQ.empty() || prQ.top()!=y) pri=;
if(!prQ.empty()) prQ.pop();
}
}
} check();
} return ;
}

[UVA] 11995 - I Can Guess the Data Structure! [STL应用]的更多相关文章

  1. UVA - 11995 - I Can Guess the Data Structure! STL 模拟

    There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...

  2. UVa 11995:I Can Guess the Data Structure!(数据结构练习)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  3. UVA 11995 I Can Guess the Data Structure!(ADT)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  4. STL UVA 11995 I Can Guess the Data Structure!

    题目传送门 题意:训练指南P186 分析:主要为了熟悉STL中的stack,queue,priority_queue,尤其是优先队列从小到大的写法 #include <bits/stdc++.h ...

  5. UVa 11995 I Can Guess the Data Structure!

    做道水题凑凑题量,=_=||. 直接用STL里的queue.stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空. #include ...

  6. uva 11995 I Can Guess the Data Structure stack,queue,priority_queue

    题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...

  7. UVA - 11995 I Can Guess the Data Structure!(模拟)

    思路:分别定义栈,队列,优先队列(数值大的优先级越高).每次放入的时候,就往分别向三个数据结构中加入这个数:每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结 ...

  8. uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)

    11995 - I Can Guess the Data Structure! There is a bag-like data structure, supporting two operation ...

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

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

随机推荐

  1. Portal技术介绍

      Portal技术介绍 Portal是web应用发展的一个重要趋势,目前几乎所有大的软件厂商都有自己的Portal产品.并且Portal技术已经形成规范.本文对Portal技术和产品进行了分析,目的 ...

  2. cf B. The Fibonacci Segment

    http://codeforces.com/contest/365/problem/B #include <cstdio> #include <cstring> #includ ...

  3. 宏汇编软件MASM51的使用

    单片机开发可以用手工汇编和机器汇编两种方法.采用手工汇编就是先编写出汇编程序,然后对照单片机汇编表手工将汇编程序翻译成机器码,最后将机器码一个一个地送入开发仿真器的RAM中去进行调试. 由于采用手工汇 ...

  4. Qt在Windows下的三种编程环境搭建

    尊重作者,支持原创,如需转载,请附上原地址:http://blog.csdn.net/libaineu2004/article/details/17363165 从QT官网可以得知其支持的平台.编译器 ...

  5. POJ 3280 Cheapest Palindrome 简单DP

    观察题目我们可以知道,实际上对于一个字母,你在串中删除或者添加本质上一样的,因为既然你添加是为了让其对称,说明有一个孤立的字母没有配对的,也就可以删掉,也能满足对称. 故两种操作看成一种,只需要保留花 ...

  6. 如何查找到文件以后,带目录一起拷贝到新的目录? cp --parents source destination

    如何查找到文件以后,带目录一起拷贝到新的目录? cp --parents  source  destination

  7. 【转】Ubuntu 10.04 LTS 的窗口控制按钮从左上角调整到右上角

    原文网址:http://www.linuxidc.com/Linux/2010-05/26111.htm 升级到Ubuntu 10.04后最大的问题,是最小最大和关闭按钮,放到了左边.这叫Ubuntu ...

  8. Jquery回车键切换焦点方法(兼容各大浏览器)

    做项目时,客户要求能够用enter回车直接切换输入(焦点),当最后一个时候,直接提交信息. 第一想法就是,网上去copy一段代码直接用.但了百度.谷歌找了个遍,找到的代码80%以上都是一样的.有的代码 ...

  9. 黑马程序员_Java面向对象2_继承

    4.面向对象_继承 4.1继承的概述 提高了代码的复用性. 让类与类之间产生了关系,有了这个关系,才有多态的特性. 注意:千万不要为了获取其他类的功能而去继承,简化代码而继承.必须是类与类之间有所属关 ...

  10. Java里的equals总结

    前段时间一直在工作中使用Java,由于有一些C++功底,于是简单看了一下Java相关的语法便开始编写代码,结果在创建一个自定义类,并将自定义类放入ArrayList中,之后查找ArrayList是否有 ...