UVA 11995 STL 使用
2016-04-20 20:52
447 查看
There is a bag-like data structure, supporting two operations:
Throw an element x into the bag.
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!
It's definitely a stack.
It's definitely a queue.
It's definitely a priority queue.
It can't be a stack, a queue or a priority queue.
It can be more than one of the three data structures mentioned above.
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 题意: 判断是哪一种数据结构 题解: STL <queue> <stack> priority_queue
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> using namespace std; queue<int> q; stack<int> s; struct ss{ int f; friend bool operator < (ss a, ss b) {return a.f < b.f;} }; priority_queue<ss> pq; int aa,bb; int n; int ans,ans1,ans2,ans3; int cun[1005][2]; int main() { while(scanf("%d",&n)!=EOF) { ans=0;ans1=0;ans2=0;ans3=0; while(q.size()>0) q.pop(); while(s.size()>0) s.pop(); while(pq.size()>0) pq.pop(); memset(cun,0,sizeof(cun)); for(int i=1;i<=n;i++) scanf("%d %d",&cun[i][0],&cun[i][1]); for(int i=1;i<=n;i++) { int aa=cun[i][0]; int bb=cun[i][1]; if(aa==1) { q.push(bb); s.push(bb); struct ss gg; gg.f=bb; pq.push(gg); } else { ans++; if(!q.empty()&&bb==q.front()) { ans1++; q.pop();} if(!s.empty()&&bb==s.top()) {ans2++;s.pop();} if(!pq.empty()&&bb==pq.top().f) {ans3++;pq.pop();} } } //cout<<ans<<" "<<ans1<<" "<<ans2<<" "<<ans3<<" "<<endl; int qu=0,st=0,pri=0; if(ans==ans1) qu=1; if(ans==ans2) st=1; if(ans==ans3) pri=1; if (st + qu + pri > 1) printf("not sure\n"); else if (st) printf("stack\n"); else if (qu) printf("queue\n"); else if (pri) printf("priority queue\n"); else printf("impossible\n"); } return 0; } /* 3 1 1 1 2 2 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 */
相关文章推荐
- 20个很有用的CSS技巧
- ASP.NET 使用RegularExpressionValidator控件(数据输入格式验证)
- MySQL应用并发优化实践
- 顶部下拉广告
- golang 生成RSA公钥和私钥文件
- 把远程MySQL数据库中的数据手工导入到本地MySQL数据库
- Mybatis最入门---ResultMaps实例篇(一对一查询)
- vim配置及插件安装管理(超级详细)
- bzoj 3745: [Coci2015]Norma 分治&数学
- 指弹 奇迹的山 原版
- Google Gallery for Android 4.4源码分析以及主UI修改——Gallery整体结构
- 人活着系列之寻找最完美的人生(sdut_2937)
- 第2章 算法分析
- PL/0 词法分析器
- spring错误汇总
- 为什么要用枚举实现Singleton--java学习笔记
- 向eclipse中导入源码教程详解
- android面试中经典的75道逻辑思维题
- Hybrid App开发模式中, IOS/Android 和 JavaScript相互调用方式
- bzoj4538 [Hnoi2016]网络