链接:https://ac.nowcoder.com/acm/contest/1072/F
来源:牛客网
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…??: Print the numbers of the cows in the line in order from left to right, one number per line.
示例1
输入:
10
A L
A L
A R
A L
D R 2
A R
A R
D L 1
A L
A R
输出:
7
2
5
6
8
提示:
Input Resulting Cow Line
A L 1
A L 2 1
A R 2 1 3
A L 4 2 1 3
D R 2 4 2
A R 4 2 5
A R 4 2 5 6
D L 1 2 5 6
A L 7 2 5 6
A R 7 2 5 6 8
题意思路:*一头牛进入该行(一个参数指示是在左侧还是右侧)。
*k牛从左侧或右侧离开该行(提供的参数定义牛的数量和哪一侧)。
输入行从不请求无法执行的操作。处理完所有输入行后,程序应该按从左到右的顺序打印行中的奶牛。最后一行在输入规范结束时保证是非空的。
输入:
*第1行:单个整数:s
*第2到S+1行:第I+1行包含四种格式之一的规范I:
A L— 一头牛在线的左边加入了队伍
A R— 一头牛从线的右边加入队伍
*D L K— K头牛从线路左侧离开
*D R K—K头牛从线路右侧离开
输出:
第1行…?:按从左到右的顺序打印行中的奶牛数量,每行一个数字。(结合提示就可以明白题意,接触了stl后发现用双向队列真的简单。
代码:
#include<bits/stdc++.h>
using namespace std ;
deque<int>q;
int main()
{
int c,n,m,t,cnt=0,i;
char a,b;
cin>>t;
while(t--)
{
cin>>a>>b;
if(a=='A')
{
if(b=='L')
{
q.push_front(++cnt);
}
else
{
q.push_back(++cnt);
}
}
else
{
cin>>c;
if(b=='L')
{
for(i=1;i<=c;i++)
{
q.pop_front();
}
}
else
{
for(i=1;i<=c;i++)
{
q.pop_back();
}
}
}
}
while(!q.empty())
{
cout<<q.front()<<endl;
q.pop_front();
}
return 0;
}
感悟:数据结构很重要,要好好学。