Codevs_P3186 队列练习2
2015-11-03 20:08
267 查看
题目描述 Description
(此题与队列练习1相比改了2处:1加强了数据 2不保证队空时不会出队)
给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请
输出最终的队头元素。 操作解释:1表示入队,2表示出队
输入描述 Input Description
N(操作个数)
N个操作(如果是入队则后面还会有一个入队元素)
具体见样例(输入保证队空时不会出队)
输出描述 Output Description
最终队头元素,若最终队空,或队空时有出队操作,输出”impossible!”(不含引号)
样例输入 Sample Input
3
1 2
2
2
样例输出 Sample Output
impossible!
数据范围及提示 Data Size & Hint
对于100%的数据 N≤100000 元素均为正整数且小于等于10^8
(此题与队列练习1相比改了2处:1加强了数据 2不保证队空时不会出队)
给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请
输出最终的队头元素。 操作解释:1表示入队,2表示出队
输入描述 Input Description
N(操作个数)
N个操作(如果是入队则后面还会有一个入队元素)
具体见样例(输入保证队空时不会出队)
输出描述 Output Description
最终队头元素,若最终队空,或队空时有出队操作,输出”impossible!”(不含引号)
样例输入 Sample Input
3
1 2
2
2
样例输出 Sample Output
impossible!
数据范围及提示 Data Size & Hint
对于100%的数据 N≤100000 元素均为正整数且小于等于10^8
#include<cstdio> #include<queue> using namespace std; queue<int> a; int n,x,y; int main(){ scanf("%d",&n); while(n--){ scanf("%d",&x); if(x==1){ scanf("%d",&y); a.push(y); } else if(!a.empty()) a.pop(); else {printf("impossible!\n");return 0;} } if(a.empty()) printf("impossible!\n"); else printf("%d",a.front()); }
相关文章推荐
- C#线程队列用法实例分析
- 算法系列15天速成 第九天 队列
- 浅析STL中的常用算法
- STL区间成员函数及区间算法总结
- C语言单链队列的表示与实现实例详解
- mysql 队列 实现并发读
- C#队列Queue用法实例分析
- C#多线程处理多个队列数据的方法
- C语言循环队列的表示与实现实例详解
- C++循环队列实现模型
- C#内置队列类Queue用法实例
- Array栈方法和队列方法的特点说明
- c++ STL容器总结之:vertor与list的应用
- C++在成员函数中使用STL的find_if函数实例
- C#通过链表实现队列的方法
- 关于STL中list容器的一些总结
- C#队列Queue多线程用法实例
- C#数据结构与算法揭秘五 栈和队列
- C++中队列的建立与操作详细解析
- PHP实现的memcache环形队列类实例