DBP - Burned Pancakes Tower

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 N 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 k pancakes over as a unit (so the k-th pancake is now on top and the pancake previously on top is now in the k-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 flips, which converts a given stack of pancakes to a sorted stack with burned sides down.

Input

The first line of the input contains a single integer N < 55, the number of problem instances to follow. Each of the following N lines gives a separate dataset as a sequence of numbers separated by spaces. The first number on each line gives the number M of pancakes in the data set. The remainder of the data set is the numbers 1 through M 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 (+). M will be, at most, 1000.

Output

For each dataset, you should generate one line of output with the following values: The number of flips (K, where 0 <= K <= 3000) required to sort the pancakes and a sequence of K 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 and your task is to find the shortest one.

Score

For each test case is (3M-1)/(K+1)

Example

Input:
5
12 -2 -1 -10 +7 -4 -8 +12 -3 +5 +11 +6 -9
11 +2 +6 -10 +4 -1 -5 +7 -8 -3 +9 -11
11 +1 +8 +11 +10 +4 +3 -5 +2 -6 -7 -9
13 -2 -12 +8 +9 +10 -6 -5 +3 -7 -4 +11 -1 +13
12 +11 -3 +7 -5 -8 -12 -9 -1 +4 -2 -10 +6

Output:
25 7 12 3 1 11 4 10 2 1 9 3 1 8 6 7 2 1 6 4 5 1 1 4 1 3 
25 11 1 11 3 1 10 1 1 9 7 1 8 2 1 6 1 5 3 4 2 1 3 1 2 1 
25 3 11 8 1 10 3 9 7 1 8 7 1 7 6 1 6 4 1 5 3 4 1 1 3 1 
24 2 1 12 2 1 11 4 10 3 7 2 1 6 4 5 2 1 4 1 3 1 1 2 1 
29 6 1 12 7 11 6 10 1 1 9 5 1 8 6 7 5 6 2 5 2 1 4 1 1 3 1 1 2 1 

Score:
6.4943590

Added by:Ruslan Sennov
Date:2010-02-18
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC PERL6 SQLITE VB.NET
Resource:Soccer Choreography, Flipping Burned Pancakes

hide comments
2013-06-19 08:42:22 Maciej Misiak
For Perl / bash I get TLE for trivial solution for less than 20 tests. Pity that scripting languages cannot be used.
2010-10-01 18:25:13 Mitch Schwartz
A bit pedantic, but K is not really the number of flips required for sorting, rather it is a number of flips sufficient for sorting. Otherwise there would be only one possible score for AC, and sub-optimal sorting would give WA.

Last edit: 2010-10-01 18:39:28
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.