Flipping Burned Pancakes(HDU-1988)

G - Flipping Burned Pancakes

 

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.

Example

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

题目大意:有一摞大小不一的烙饼,正负代表饼的正反面,通过题目规定的翻转方式,将这摞饼整理成自上而下从小到大,并且全   部为正面。题目规定的合法翻转方式需满足下列两个条件:
①可以同时拿多个饼整体翻转,此时他们的符号都需要改变。
②只能翻转这一堆饼的“顶层”——例如,最上边的3层,最上边的7层,或最上边的一层,也就是说不可以直接从第三层   或其他的下层开始翻……(我就因为一开始忽略了这一点wa了好几次 大哭)。
 


解题思路:例如-3、+1、-2、-5、+4这一堆饼,要想把第一个-3翻到他应该在的位置(第三层),必定会牵扯到其他位置的饼,途径   的+1、-2的位置符号都会受到影响,就算-3已经被安置好了,在后续的翻转中也会使-3的位置再次被挪动,考虑到这个   问题,很容易想到应该先把下层的饼安排好,防止二次挪动。



具体步骤:
①找到大小为 i 的饼,把他连同他之上,所有x个饼全部翻转,这样的话,这个饼就翻到最上边来了……
②判断一下此刻这个饼是不是正面朝上,考虑到一会还要把它翻到第i层上去,所以他要是正面朝上就得把它翻一下。
③把它翻到第i层上去,继续翻转大小为 i+1 的饼。
④重复以上步骤直到第二层,这个时候第一层肯定是大小为1的饼了,判断一下它是否正面朝上,不是的话翻过来……
⑤代码中的 a[i] 记录第i层饼的大小,b[i] 记录大小为 i 的饼目前在第几层,队列q记录结果。


  
#include<stdio.h>
#include<queue>
using namespace std;
queue<int>q;
int a[35],b[35];
void function();
void swap(int x,int y);
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1 ; i<=n ; i++)
	{
		printf("%d ",i);
		function();
	}
	return 0;
}
void swap(int x,int y)
{
	int t;
	if(x==y)
	return ;
	q.push(y-x+1);
	while(x<y)
	{
		t = a[x];
		a[x] = -a[y];
		a[y] = -t;
		b[a[x]>0?a[x]:(-a[x])] = x;
		b[a[y]>0?a[y]:(-a[y])] = y;
		x++;
		y--;
	}
	if(x==y)
	a[x]*=-1;
}
void function()
{
	int n,t;
	scanf("%d",&n);
	for(int i=1 ; i<=n ; i++)
	{
		scanf("%d",&t);
		a[i] = t;
		t=t>0?t:(-t);
		b[t] = i;
	}
	for(int i=n ; i>=2 ; i--)
	{
		if(a[i]==i && a[i]>0)
		continue; 
		swap(1,b[i]);
		if(a[1] > 0)
		{
			q.push(1);
			a[1]*=-1;
		}
		swap(1,i);
	}
	if(a[1]<0)
	q.push(1);
	printf("%d",q.size());
	while(!q.empty())
	{
		printf(" %d",q.front());
		q.pop();
	}
	printf("\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值