南邮 OJ 1854 B. Give you a Bag
来源:互联网 发布:域名定义 编辑:程序博客网 时间:2024/05/17 04:22
B. Give you a Bag
总提交 : 51 测试通过 : 22
比赛描述
有这样一种类似于闭包的数据结构,支持两种操作:
1 x
把元素 x 放入 这个包内
2
从包内丢弃一个元素
现在我们给你一个操作带返回值的操作序列,我们需要你猜这个数据结构是什么。
你有以下选择:
Stack 栈(先进后出)
Queue 队列(先进先出)
Priority-queue 优先权队列( 每次都丢弃最大元素的最大堆 )
输入
有多组测试用例。每个测试案例开始行包含一个整数N(1 <= N <=1000)。以下N行,每行一个操作。 对于操作一,输入为1 x,x代表将要放入数据结构里面的数,对于操作二,输入2 x,这里的x代表返回值,即执行操作2之后,返回的一个值(例如优先权队列,则返回的是最大值)。x的值始终是一个正整数不大于100。输入到文件尾。输入文件的大小不超过1MB。
输出
对于每个测试用例,输出下面语句中的其中一个:
stack:表示判断这一定是一个堆栈。
queue:表示判断这一定是一个队列。
priority queue: 表示判断这一定是一个优先级队列。
impossible: 表示判断它不可能是一个堆栈,队列或优先级队列。
not sure: 表示判断它可能是以上所说三种数据结构中的多种。
样例输入
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
样例输出
queue
not sure
impossible
stack
priority queue
题目来源
ACM爱好者协会
/* 98MS#include<iostream>#include<stack>#include<queue>using namespace std;int main(){//freopen("test.txt","r",stdin);int n,a,b,count,sum;stack<int> si;queue<int> qi;priority_queue<int> pqi;bool stack_flag,queue_flag,pq_flag,ok;while(scanf("%d",&n)==1){while(si.size()){si.pop();}while(qi.size()){qi.pop();}while(pqi.size()){pqi.pop();}count = 3;sum = 0;stack_flag = queue_flag = pq_flag = ok = 1;while(ok && count && n--){scanf("%d%d",&a,&b);if(a==1){sum++;if(stack_flag){si.push(b);}if(queue_flag){qi.push(b);}if(pq_flag){pqi.push(b);}}else{ok = 0;if(--sum<0){count = 0;break;}if(stack_flag){if(si.top()==b){si.pop();ok = 1;}else{stack_flag = 0;count--;}}if(queue_flag){if(qi.front()==b){qi.pop();ok = 1;}else{queue_flag = 0;count--;}}if(pq_flag){if(pqi.top()==b){pqi.pop();ok = 1;}else{pq_flag = 0;count--;}}}}while(n-->0){scanf("%*d%*d");}if(!ok){printf("impossible\n");}else if(count>1){printf("not sure\n");}else if(count==1){if(stack_flag){printf("stack\n");}else if(queue_flag){printf("queue\n");}else{printf("priority queue\n");}}else{printf("impossible\n");}}}*/#include<iostream>#include<stack>#include<queue>using namespace std;int getNum(){int t,r;while((t=getchar())<'0' || t>'9');r = t-'0';while((t=getchar())>='0' && t<='9'){r = r*10 +t-'0';}return r;}int main(){//freopen("test.txt","r",stdin);int n,a,b,count,sum;stack<int> si;queue<int> qi;priority_queue<int> pqi;bool stack_flag,queue_flag,pq_flag,ok;while(scanf("%d",&n)==1){while(si.size()){si.pop();}while(qi.size()){qi.pop();}while(pqi.size()){pqi.pop();}count = 3;sum = 0;stack_flag = queue_flag = pq_flag = ok = 1;while(ok && count && n--){//scanf("%d%d",&a,&b);a = getNum();b = getNum();if(a==1){sum++;if(stack_flag){si.push(b);}if(queue_flag){qi.push(b);}if(pq_flag){pqi.push(b);}}else{ok = 0;if(--sum<0){count = 0;break;}if(stack_flag){if(si.top()==b){si.pop();ok = 1;}else{stack_flag = 0;count--;}}if(queue_flag){if(qi.front()==b){qi.pop();ok = 1;}else{queue_flag = 0;count--;}}if(pq_flag){if(pqi.top()==b){pqi.pop();ok = 1;}else{pq_flag = 0;count--;}}}}while(n-->0){scanf("%*d%*d");}if(!ok){printf("impossible\n");}else if(count>1){printf("not sure\n");}else if(count==1){if(stack_flag){printf("stack\n");}else if(queue_flag){printf("queue\n");}else{printf("priority queue\n");}}else{printf("impossible\n");}}}
- 南邮 OJ 1854 B. Give you a Bag
- Give you a Set
- 1154_Give you a Bag
- You give love a bad name
- 南邮 OJ 1084 A + B Problem (1)
- 南邮 OJ 1085 A + B Problem (2)
- 南邮 OJ 1086 A + B Problem (3)
- 南邮 OJ 1087 A + B Problem (4)
- OJ 1010 A + B
- A + B for you again
- Give Me a Handle, and I'll Show You an Object
- I wonder if you can give me a lift? 能让我搭一程吗
- Give Me a Handle, and I'll Show You an Object
- Can you give a visual explanation for the back propagation algorithm for neural networks?
- A + B 九度oj
- hdu-oj 1412 {A} + {B}
- hdu-oj 矩形A + B
- A+B Problem && OJ推荐
- JavaScript HTML DOM事件
- AndroidStudio开发环境搭建
- 嵌入式开发新手篇-如何成为优秀的嵌入式开发者
- Android学习笔记之网络图片加载
- 自定义Material Design风格炫酷加载进度条
- 南邮 OJ 1854 B. Give you a Bag
- hbase故障处理
- java分页实现
- Android studio用WiFi连接adb
- group_concat详解
- Android内存管理基础
- Js-最严谨的校验邮箱地址的正则表达式
- 火狐基本设置
- C++ Primer Plus第六版 第九章 编程练习答案