B
来源:互联网 发布:淘宝服装试用报告范文 编辑:程序博客网 时间:2024/05/03 22:53
Ada the Ladybug has many things to do. She puts them into her queue. Anyway she is very indecisive, so sometime she uses the top, sometime the back and sometime she decides to reverses it.
Input
The first line consists of 1 ≤ Q ≤ 106, number of queries. Each of them contains one of following commands
back - Print number from back and then erase it
front - Print number from front and then erase it
reverse - Reverses all elements in queue
push_back N - Add element N to back
toFront N - Put element N to front
All numbers will be 0 ≤ N ≤ 100
Output
For each back/front query print appropriate number.
If you would get this type of query and the queue would be empty, print "No job for Ada?" instead.
Example Input
15toFront 93frontbackreversebackreversetoFront 80push_back 53push_back 50frontfrontreversepush_back 66reversefront
Example Output
93No job for Ada?No job for Ada?805366
解析:双向队列模拟;主要是reverse,定义一个flag标记,对头队尾即可;
#include<bits/stdc++.h>using namespace std;int Q;char s[15];int num;deque<int>q;deque<int>qq;deque<int>::iterator it;int main(){ int ans=0; scanf("%d",&Q); for(int i=0;i<Q;i++) { scanf("%s",s); if(s[0]=='t') { scanf("%d",&num); if(ans%2==0) q.push_front(num); else q.push_back(num); } else if(s[0]=='p') { scanf("%d",&num); if(ans%2==0) q.push_back(num); else q.push_front(num); } else if(s[0]=='f') { if(q.empty()) { puts("No job for Ada?"); } else { if(ans%2==0) { int top=q.front(); q.pop_front(); printf("%d\n",top); } else { int tail=q.back(); q.pop_back(); printf("%d\n",tail); } } } else if(s[0]=='b') { if(q.empty()) { puts("No job for Ada?"); } else { if(ans%2==0) { int tail=q.back(); q.pop_back(); printf("%d\n",tail); } else { int top=q.front(); q.pop_front(); printf("%d\n",top); } } } else { ans++; } }}