Title Description
Farmer John ' s N cows (conveniently numbered 1..N) is forming a line. The line begins with no cows and then, as time progresses, one by one, the cows joins the line to 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 F Avorite Pasture.
FJ have trouble keeping track of all the cows in the line. Please help him.
The cows enter the numerical order 1..N, and once a cow leaves the line she never re-enters it. Your program would be given s (1 <= s <= 100,000) input specifications; Each appears on a, and is one of the 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, can not is performed.
After all the input lines has been processed, your program should print the cows in the line in order from left to right. The final line was guaranteed to being non-empty at the end of the input
Specifications.
John's N Cows (numbered 1 to N) line up in a straight line. There was not a cow at the beginning of the line. The next S (1≤s≤100000) event occurs, and an event may be one of the following four scenarios:
. A cow joins the left side of the queue (enter "AL").
. A cow joins the right side of the team (enter "AR").
· K only the troop left cow left (enter "DLK").
· K Only the troop right cow leaves (enter "DRK").
Ask what the last team is.
The data guarantees that the cows leaving will not exceed the number of cows in the team, and the last team is not empty input format input output format :
Line 1: A single integer:s
Lines 2..s+1:line i+1 contains specification I in one of the 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
Line 1th enters S, followed by the S line each line describes an event, as shown in the title description
Output format:
Lines 1..?: Print The numbers of the cows in the "the" in the "order from" to "right", one number per line.
From left to right the final situation of the team output.
input and Output sample input Example # #:
Ten
A l
a l
a r
a l
D r 2
a r
a r
d l 1
a l
a R
Sample # # of output:
7
2
5)
6
8
Description
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
It's probably just a review of the double-ended queue.
Deque no action for size () ... Orz
#include <bits/stdc++.h>
using namespace std;
Template <typename t> void Read (T &x) {
x=0;int F=1;char Ch=getchar ();
for (;! IsDigit (CH); Ch=getchar ()) if (ch== '-') f=-1;
for (; isdigit (ch); Ch=getchar ()) x=x*10+ch-' 0 ';
x*=f;
}
int n;
int x;
int Curr;
Deque<int> dq;
String str;
int main () {
scanf ("%d", &n);
for (int i=1;i<=n;++i) {
cin>>str;
if (str[0]== ' A ') {
cin>>str;
if (str[0]== ' L ') Dq.push_front (++curr);
else Dq.push_back (++curr);
}
else{
cin>>str>>x;
if (str[0]== ' L ') for (int j=1;j<=x;++j) Dq.pop_front ();
else for (int j=1;j<=x;++j) dq.pop_back ();
}
}
while (!dq.empty ()) {
x=dq.front ();
printf ("%d\n", x);
Dq.pop_front ();
}
return 0;
}