[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 ...
随机推荐
- 工程和界面—Webstorm入门指南 Webstorm中的工程-备
1.新建工程 “Quick Start”界面新建工程: 也可以点击顶部菜单栏“File”-> “New Project”. 弹出如下界面: “Location”指向想要创建的工程目录(如果该目录 ...
- bash模式和模式匹配
bash模式和模式匹配 ${variable#pattern} 如果模式匹配变量取值的开头,删除最短匹配部分,返回其余部分 ${variable##pattern} 如果模式匹配变量取值的开头,删除最 ...
- documentElement vs body
document.documentElement与document.body. 2011-03-12 10:15:46| 分类: javascript|字号 订阅 这是DOMDocument ...
- 一定要学会OutputDebugString,方便源码级调试
省得到处自己print,麻烦的要死...
- android中button点击频率控制
public class Utils { private static long lastClickTime; public static boolean isFastDoubleClick() { ...
- Android Animations简介
一 .Animations简介 Animations提供了一系列的动画效果,这些效果可以应用于绝大多数的控件: 二.Animations的分类 第一类:TweenedAnimations,该类Anim ...
- 【活动】明星衣橱CEO林清华聊创业 | 猎云网
[活动]明星衣橱CEO林清华聊创业 | 猎云网 [活动]明星衣橱CEO林清华聊创业
- membership source code
You can find their source code in codeplex at the ASP.NET source code. ExtendedMembershipProvider: h ...
- Leetcode:linked_list_cycle
一. 题目 给定一个链表.确定它是否有一个环.不使用额外的空间? 二. 分析 1. 空链表不成环 2. 一个节点自环 3. 一条链表完整成环 思路:使用两个指针,一个每次往前走2步,一 ...
- mysql常用操作(转自阿铭linux)
在前面两个章节中已经介绍过MySQL的安装了,但是光会安装还不够,您还需要会一些基本的相关操作.当然了,关于MySQL的内容也是非常多的,只不过对于linux系统管理员来讲,一些基本的操作已经可以应付 ...