[UVA] 11995 - I Can Guess the Data Structure! [STL应用]
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应用]的更多相关文章
- 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. ...
- 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 ...
- 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 ...
- STL UVA 11995 I Can Guess the Data Structure!
题目传送门 题意:训练指南P186 分析:主要为了熟悉STL中的stack,queue,priority_queue,尤其是优先队列从小到大的写法 #include <bits/stdc++.h ...
- UVa 11995 I Can Guess the Data Structure!
做道水题凑凑题量,=_=||. 直接用STL里的queue.stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空. #include ...
- uva 11995 I Can Guess the Data Structure stack,queue,priority_queue
题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...
- UVA - 11995 I Can Guess the Data Structure!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高).每次放入的时候,就往分别向三个数据结构中加入这个数:每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结 ...
- 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 ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
随机推荐
- NSNumber与NSInteger的区别 -bei
基本类型,如同C 语言中的 int 类型一样,拿来就可以直接用. 而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值. 类的初始化,需用类自身的方法 (类方法). 代码中所创建的对 ...
- QT中读取文本数据(txt)
下面的代码实现读取txt文档中的数据,并且是一行一行的读取. void MainWindow::on_pushButton_clicked() { QFile file("abcd.txt& ...
- docker 1.10.3 里php出现 curl 56错误码问题解决
http://www.cnblogs.com/fengwei/p/5899018.html
- (转) Linux的capability深入分析(2)
一)capability的工具介绍 在我们的试验环境是RHEL6,libcap-2.16软件包中包含了相关的capability设置及查看工作,如下: rpm -ql libcap-2.16- ...
- 【转】Android 之 下拉框(Spinner)的使用
原文网址:http://imshare.iteye.com/blog/770950 下拉列表 Spinner. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下拉列 ...
- VS项目如何添加到svn
SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.这里就讲一下VS2010如何将项目导入SVN版本控制. 工具 ...
- Struts2简单例子
Struts实现注册功能 ControlFilter.java package com.jikexueyuan.filter; import java.io.IOException; import j ...
- hdu 5391 Zball in Tina Town(打表找规律)
问题描述 Tina Town 是一个善良友好的地方,这里的每一个人都互相关心. Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大11倍.在第二天的时候, ...
- Log4j使用说明
Log4J实例应用开发 在实际编程时,要使Log4j真正在系统中运行事先还要对配置文件进行定义.定义步骤就是对Logger.Appender及Layout的分别使用.Log4j支持两种配置文件格式,一 ...
- gnuplot常用技巧
一. 基础篇: 在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. 1.plot命令 gnuplot> plot sin(x) with l ...