码迷,mamicode.com
首页 > 其他好文 > 详细

codevs 3186 队列练习2

时间:2016-08-08 19:15:41      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

3186 队列练习 2

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 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

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int a[100010],n;
int main()
{
int x,y,head=0,tail=0;
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%d",&y);
if(y==1)
{
scanf("%d",&x);
a[tail]=x;
tail++;
}
if(y==2)
{
head++;
}
if(head>=tail) {
printf("impossible!\n");
return 0;
}


}
if(head>=tail) {
printf("impossible!\n");
}
else
printf("%d\n",a[head]);
return 0;
}(A了90分)

codevs 3186 队列练习2

标签:

原文地址:http://www.cnblogs.com/zhishenduchuang/p/5750520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!