题目描述
Farmer John's N cows (conveniently numbered 1..N) are forming a line. The line begins with no cows and then, as time progresses, one by one, the cows join the line on the left or right side. Every once in a while, some number of cows on the left or right side of the line all leave the line to go graze in their favorite pasture.
FJ has trouble keeping track of all the cows in the line. Please help him.
The cows enter the line in numerical order 1..N, and once a cow leaves the line she never re-enters it. Your program will be given S (1 <= S <= 100,000) input specifications; each appears on a single line and is one of two types:
* A cow enters the line (a parameter indicates whether on the left or right).
* K cows leave the line from the left or right side (supplied parameters define both the number of cows and which side).
Input lines never request an operation that can not be performed.
After all the input lines have been processed, your program should print the cows in the line in order from left to right. The final line is guaranteed to be non-empty at the end of the input specifications.
输入格式
* Line 1: A single integer: S
* Lines 2..S+1: Line i+1 contains specification i in one of four formats:
* A L -- a cow arrives on the Left of the line
* A R -- a cow arrives on the Right of the line
* D L K -- K cows depart the Left side of the line
* D R K -- K cows depart the Right side of the line
输出格式
* Lines 1..n: Print the numbers of the cows in the line in order from left to right, one number per line.
题意翻译
Farmer John(以下简称 FJ)的 N 头奶牛(用 1…N 编号)在直线上排队。一开始,这条线上没有任何奶牛,随着时间的推移,奶牛们会一个接一个地站到队伍的左边或右边。又过了一会儿,某些奶牛会从队伍里离开,去吃自己最喜欢的草料。
FJ 无法跟踪每一头奶牛,于是,他想让你来帮助他。
奶牛以 1…N 的顺序排队,并且离开的奶牛不会再次回来。数据将会给出 S(1≤S≤100000) 条指令,各占一行,分两种:
- A 头奶牛加入了队列(还有一个参数,表示从左加入还是从右加入);
- K 头奶牛从左边或者右边离开了队列(还有两个参数,分别表示从左离开还是从右离开和离开多少头奶牛)。
输入的命令一定是可以执行的。
所有的操作结束后,你的程序应该以从左到右的顺序输出这个奶牛队列。数据保证最后的队列不空。
【输入格式】
- 第 11 行:单独一个整数 S。
- 第 2…S+1 行;每行会有一条命令,有以下几种:
A L
:一头奶牛从队列左边加入;A R
:一头奶牛从队列右边加入;D L K
:K 头奶牛从队伍左边离开;D R K
:K 头奶牛从队伍右边离开。
【输出格式】
- 第 1… 行:从左到右输出最后的奶牛队列,一个奶牛编号占一行。
#include<bits/stdc++.h>
using namespace std;
int main(){
int s;
cin>>s;
deque<int>ans;
int now=1;
while(s--){
char a;
cin>>a;
if(a=='A'){
cin>>a;
if(a=='L'){
ans.push_front(now);
now++;
}
if(a=='R'){
ans.push_back(now);
now++;
}
}
if(a=='D'){
cin>>a;
if(a=='L'){
int x;
cin>>x;
while(x--)ans.pop_front();
}
if(a=='R'){
int x;
cin>>x;
while(x--)ans.pop_back();
}
}
}
for(int i=0;i<ans.size();i++)cout<<ans[i]<<"\n";
}