Coupling trains at the yard
At a rail yard, cars are coupled in one row to make a train.
At first there are no cars.
Process n commands in order.
Cars go on and come off at both the front and the back of the row, so use the standard library's double-ended queue (deque) container.
- addf x: couple car number x at the very front.
- addb x: couple car number x at the very back.
- cutf k: uncouple the front car, k times.
If fewer than k cars are left, uncouple all of them and stop.
Print the numbers of the uncoupled cars in the order they were uncoupled (front car first) on one line, separated by single spaces.
If no car was uncoupled, print -.
- cutb k: uncouple the back car, k times.
The other rules are the same as cutf; since the order is the order of uncoupling, print the back car first.
- rot k: k times, uncouple the front car and couple it at the back.
Print nothing.
If there are no cars, do nothing.
After all commands, print the number of cars left, then on the next line the numbers of the cars left from the front, separated by single spaces.
If no cars are left, print - on the second line.
- Input
- First line n (1 ≤ n ≤ 5000).
Then one command per line for n lines.
For addf/addb 1 ≤ x ≤ 10^9, for cutf/cutb 1 ≤ k ≤ 10^9, for rot 0 ≤ k ≤ 10^9.
Car numbers may repeat. - Output
- One line per cutf/cutb (the uncoupled car numbers, or -), then the number of cars left, then their numbers (or -)
9 addb 1 addb 2 addf 3 rot 1 cutb 2 addb 9 addf 4 rot 4 cutf 1
3 2 1 2 9 4
5 cutf 3 addf 7 addf 8 cutb 5 rot 2
- 7 8 0 -