Sicily 1514. Flipping Burned Pancakes

1514. Flipping Burned Pancakes

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge

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]  $ \Rightarrow$  +3 -1 -2 [flip 1]  $ \Rightarrow$  -3 -1 -2 [flip 3]  $ \Rightarrow$

 

+2 +1 +3 [flip 1]  $ \Rightarrow$  -2 +1 +3 [flip 2]  $ \Rightarrow$  -1 +2 +3 [flip 1]  $ \Rightarrow$  +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 ofK 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 instance3 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

Problem Source

Greater New York 2007

// Problem#: 1514
// Submission#: 3593479
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX_PROBS   1000
#define MAX_PANCAKES    30

char inbuf[256];
int nPancakes;
int pancakes[MAX_PANCAKES];
int moves[3*MAX_PANCAKES];
int curLoc[MAX_PANCAKES+1];

int parseInput(char *pIn)
{
    char *p;
    int i, ret, val, ind;
    /* get number of pancakes */
    p = pIn;
    while((*p != 0) && (isspace(*p))) p++;
    if(*p == 0)
    {
        return -2;
    }
    ret = atoi(p);
    if((ret < 1) || (ret > MAX_PANCAKES))
    {
        return -1;
    }
    for(i = 1; i <= ret; i++) curLoc[i] = 0;
    /* read ret pancakes */
    for(i = 0; i < ret ; i++)
    {
        /* skip past prev item */
        while((*p != 0) && (!isspace(*p))) p++;
        while((*p != 0) && (isspace(*p))) p++;
        if(*p == 0)
        {
            return -2;
        }
        if((*p != '+') && (*p != '-'))
        {
            return -3;
        }
        val = atoi(p);
        if((val == 0) || (val < -ret) || (val > ret))
        {
            return -4;
        }
        ind = abs(val);
        if(curLoc[ind] != 0)
        {
            return -5;
        }
        pancakes[i] = val;
        curLoc[ind] = i+1;
    }
    /* check all seen */
    for(i = 1; i <= ret; i++)
    {
        if(curLoc[i] == 0)
        {
            return -6;
        }
    }
    nPancakes = ret;
    return ret;
}

/* flip top k pancakes */
void flip(int k)
{
    int i, j, tmp;
    i = 0; j = k-1;
    while(i < j)
    {   /* swap values and change signs */
        tmp = -pancakes[i];
        pancakes[i] = -pancakes[j];
        pancakes[j] = tmp;
        i++;
        j--;
    }
    if(i == j)
    {
        pancakes[i] = -pancakes[i];
    }
    /* fix locations */
    for(i = 1; i <= nPancakes; i++)
    {
        if(curLoc[i] <= k)
        {
            curLoc[i] = k + 1 - curLoc[i];
        }
    }
}

/* for each size from largest to smallest, if not correct, flip to top
 * make burned side up, flip to correct position
 */
int FindSoln()
{
    int curPk, curStep;

    curStep = 0;
    for(curPk = nPancakes ; curPk > 0 ; curPk--)
    {
        if((curLoc[curPk] != curPk) || (pancakes[curLoc[curPk]-1] < 0))
        {   /* not in correct place or correct orientation */
            if(curPk > 1)
            {
                /* flip from cur loc to top */
                if(curLoc[curPk] != 1)
                {
                    moves[curStep++] = curLoc[curPk];
                    flip(curLoc[curPk]);
                }
                if(pancakes[0] > 0)
                {
                    moves[curStep++] = 1;
                    flip(1);
                }
                moves[curStep++] = curPk;
                flip(curPk);
            }
            else
            {
                moves[curStep++] = 1;
                flip(1);
            }
        }
    }
    return curStep;
}

/* print nStep followed by nStep moves */
void PrintSoln(int probNum, int nStep)
{
    int i;
    printf("%d %d", probNum, nStep);
    for(i = 0; i < nStep ; i++)
    {
        printf(" %d", moves[i]);
    }
    printf("\n");
}

int main()
{
    int probCnt, curProb, ret;

    if(fgets(&(inbuf[0]), 255, stdin) == NULL)
    {
        fprintf(stderr, "read failed on problem count\n");
        return -1;
    }
    inbuf[255] = 0;
    probCnt = atoi(&(inbuf[0]));
    if((probCnt < 1) || (probCnt > MAX_PROBS))
    {
        fprintf(stderr, "Problem count %d not in range 1...%d\n", probCnt, MAX_PROBS);
        return -2;
    }
    for(curProb = 1; curProb <= probCnt ; curProb++)
    {
        if(fgets(&(inbuf[0]), 255, stdin) == NULL)
        {
            fprintf(stderr, "read failed on problem %d\n", curProb);
            return -3;
        }
        if((ret = parseInput(&(inbuf[0]))) < 0)
        {
            fprintf(stderr, "parseInput returned %d on problem %d\n", ret, curProb);
            return -4;
        }
        ret = FindSoln();
        PrintSoln(curProb, ret);
    }
    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值