#P2952. [USACO09OPEN] Cow Line S

    ID: 1998 Type: RemoteJudge 1000ms 125MiB Tried: 0 Accepted: 0 Difficulty: 2 Uploaded By: Tags>模拟递推2009USACO单调队列

[USACO09OPEN] Cow Line S

题目描述

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.

题目大意

Farmer John(以下简称 FJ)的 NN 头奶牛(用 1N1 \dots N 编号)在直线上排队。一开始,这条线上没有任何奶牛,随着时间的推移,奶牛们会一个接一个地站到队伍的左边或右边。又过了一会儿,某些奶牛会从队伍里离开,去吃自己最喜欢的草料。

FJ 无法跟踪每一头奶牛,于是,他想让你来帮助他。

奶牛以 1N1 \dots N 的顺序排队,并且离开的奶牛不会再次回来。数据将会给出 SS1S1000001 \le S \le 100000) 条指令,各占一行,分两种:

  • AA 头奶牛加入了队列(还有一个参数,表示从左加入还是从右加入);
  • KK 头奶牛从左边或者右边离开了队列(还有两个参数,分别表示从左离开还是从右离开和离开多少头奶牛)。

输入的命令一定是可以执行的。

所有的操作结束后,你的程序应该以从左到右的顺序输出这个奶牛队列。数据保证最后的队列不空。

【输入格式】

  • 11 行:单独一个整数 SS
  • 2S+12 \dots S+1 行:第 i+1i+1 行会有一条命令,有以下几种:
    • A L:一头奶牛从队列左边加入;
    • A R:一头奶牛从队列右边加入;
    • D L KKK 头奶牛从队伍左边离开;
    • D R KKK 头奶牛从队伍右边离开。

【输出格式】

  • 1??1 \dots ?? 行:从左到右输出最后的奶牛队列,一个奶牛编号占一行。

【样例解释】

以下为输入的命令及对应的队列:

  • A L11
  • A L2,12,1
  • A R2,1,32,1,3
  • A L4,2,1,34,2,1,3
  • D R 24,24,2
  • A R4,2,54,2,5
  • A R4,2,5,64,2,5,6
  • D L 12,5,62,5,6
  • A L7,2,5,67,2,5,6
  • A R(最终序列):7,2,5,6,87,2,5,6,8
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

感谢@ ws_fuweidong 提供翻译。