Flipping Burned Pancakes

Description

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 at most (3n - 2) 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 decimal integer, N, 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, 30.

Output

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 (K, where K >= 0) 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. For instance 3 2 3 is also a solution to the first problem below.

Sample Input

3
3 +1 -3 -2
4 -3 +1 -2 -4
5 +1 +2 +3 +4 -5

Sample Output

1 6 2 1 3 1 2 1
2 6 4 1 4 3 1 2

3 3 5 1 5


这道题开始题目理解错了,一直用搜索写出来的答案不对,而且还超时了,

其实这道题只需要贪心局部最优就行了,我只需要将最大的放到最下面去,

且符号为正就不需要考虑此数了,所以从下往上翻转就行了。


#include <stdio.h>
#include <stdlib.h>
const int maxn = 35;
struct node
{
    int sign, value;
} a[maxn];
int ans[maxn*10];
void swap ( int l, int r )
{
    while ( l <= r )    //交换注意保存中间值
    {
        int t = a[l].value;
        a[l].value = a[r].value;
        a[r].value = t;
        t = a[l].sign;  //符号也需要保存
        a[l].sign = ! a[r].sign;    //相反符号
        a[r].sign = ! t;
        l ++;
        r --;
    }
}
int main ( )
{
    int T, cas = 1, cnt, n;
    char str[maxn];
    scanf ( "%d", &T );
    while ( T -- )
    {
        cnt = 0;
        scanf ( "%d", &n );
        for ( int i = 1; i <= n; i ++ )
        {
            scanf ( "%s", str );
            if ( str[0] == '+' )
                a[i].sign = 0;
            else
                a[i].sign = 1;
            a[i].value = atoi ( str+1 );
        }
        for ( int i = n; i >= 1; i -- ) //从大到小开始翻转
        {
            if ( a[i].value == i && a[i].sign == 0 )
                continue ;
            int pos;
            for ( int j = 1; j <= n; j ++ )
                if ( a[j].value == i )  //找到最大煎饼的位置
                {
                    pos = j;
                    break ;
                }
            if ( pos != 1 ) //不为1就要交换,反正要将最大的翻转的最上面
            {
                ans[cnt ++] = pos;
                swap ( 1, pos );
            }
            if ( i == 1 && a[1].sign == 0 )
            //这个条件多余,因为如果是这样上面就已经continue了
                continue ;
            if ( a[1].sign == 0 )   //第一个为正号就变为负号,因为要在翻转一次
            {
                ans[cnt ++] = 1;
                swap ( 1, 1 );
            }
            ans[cnt ++] = i;    //翻转到最下面位置
            swap ( 1, i );
        }
        printf ( "%d %d", cas ++, cnt );
        for ( int j = 0; j < cnt; j ++ )
            printf ( " %d", ans[j] );
        printf ( "\n" );
    }
    return 0;
}


你可能感兴趣的:(Flipping Burned Pancakes)