HDU 1988 Flipping Burned Pancakes (模拟)

Flipping Burned Pancakes

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 465    Accepted Submission(s): 155

Problem 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):



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
Source
Recommend
linle   |   We have carefully selected several similar problems for you:  1990 1992 1993 1991 1522 

题意:题意好难懂....看了一会...给你n块煎饼,尺寸为1至n,从上到下堆成一堆。正面朝上为+,反面朝上为-。每次可以选择上面若干块整个翻转过来。让你求翻转的每个步骤,使得最后正面朝上,尺寸从上到下是小到大的....看一下题目的图就知道了....

题解:模拟一下,从大的煎饼开始搞,让其放在最底下。。。然后不断模拟....

AC代码:
#include<bits/stdc++.h>
using namespace std;
int n, a[35] ;
int ans[110];
int num;
int find(int x)
{ 
    for (int i = 1 ; i <= n ; i++)
        if (abs(a[i]) == x) return i ;
    return 0 ;
}
void change(int pos)
{
    for(int i = 1, j = pos ; i < j ; i++, j--)
    {
       swap(a[i],a[j]);
    }
    for(int i = 1 ; i <= pos ; i++) a[i] = -a[i] ;
}
void solve()
{
    int pos ;
    num = 0 ;
    for (int i = n ; i >= 1 ; i--)
    {
        if (a[i] == i) continue ;
        pos = find(i) ;
        if (pos != 1)
        {
            change(pos);
            ans[num++] = pos;
        }
        if (a[1] > 0)
		{
            ans[num++] = 1;
            a[1] = -a[1];
        }
        if (i != 1)
        {
            ans[num++] = i;
            change(i);
        }
        else
		{
        	
            ans[num++] = 1;
            a[i] = -a[i];
        }
    }
}
int main()
{
    int t,cas = 1;
    scanf ("%d", &t) ;
    while (t--)
    {
        scanf ("%d", &n) ;
        for (int i = 1 ; i <= n ; i++)scanf("%d", &a[i]);
        
        solve();
        
        printf ("%d %d", cas++, num) ;
        for (int i = 0 ; i < num ; i++)
        {
            printf (" %d", ans[i]) ;
        }
        puts ("") ;
    }
    return 0 ;
}


 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值