1936. Flipping Burned Pancakes

单点时限: 2.0 sec

内存限制: 256 MB

The cook at the Frobbozz Magic Pancake House sometimes falls asleep on the job while cooking pancakes. As a result, one side of a stack of pancakes is often burned. Clearly, it is bad business to serve visibly burned pancakes to the patrons. Before serving, the waitress will arrange the stacks of pancakes so that the burned sides are facing down. You must write a program to aid the waitress in stacking the pancakes correctly.

We start with a stack of pancakes of distinct sizes, each of which is burned on one side. The problem is to convert the stack to one in which the pancakes are in size order with the smallest on the top and the largest on the bottom and burned side down for each pancake. To do this, we are allowed to flip the top pancakes over as a unit (so the -th pancake is now on top and the pancake previously on top is now in the -th position and the burned side goes from top to bottom and vice versa).

For example (+ indicates burned bottom, - a burned top):

+1 -3 -2 [flip 2] -->
+3 -1 -2 [flip 1] -->
-3 -1 -2 [flip 3] -->
+2 +1 +3 [flip 1] -->
-2 +1 +3 [flip 2] -->
-1 +2 +3 [flip 1] -->
+1 +2 +3

You must write a program which finds a sequence of at most flips, which converts a given stack of pancakes to a sorted stack with burned sides down.

输入格式

The first line of the input contains a single decimal integer, , the number of problem instances to follow. Each of the following lines gives a separate dataset as a sequence of numbers separated by spaces. The first number on each line gives the number, , of pancakes in the data set. The remainder of the data set is the numbers through in some order, each with a plus or minus sign, giving the initial pancake stack. The numbers indicate the relative sizes of the pancakes and the signs indicate whether the burned side is up (-) or down (+). will be, at most, .

输出格式

For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, the number of flips (, where ) required to sort the pancakes and a sequence of numbers, each of which gives the number of pancakes to flip on the corresponding sorting step. There may be several correct solutions for some datasets. For instance 3 2 3 is also a solution to the first problem below.

样例

Input
3
3 +1 -3 -2
4 -3 +1 -2 -4
5 +1 +2 +3 +4 -5
Output
1 6 2 1 3 1 2 1
2 6 4 1 4 3 1 2
3 3 5 1 5

13 人解决,19 人已尝试。

18 份提交通过,共有 41 份提交。

5.3 EMB 奖励。

创建: 11 年,10 月前.

修改: 2 年前.

最后提交: 1 年,2 月前.

来源: Greater New York 2007

题目标签