Flipping Burned Pancakes(HDU-1988)

本文介绍了一种经典的计算机科学问题——翻转煎饼问题,该问题要求通过一系列特定的翻转操作,将一组大小不一且一面烧焦的煎饼按大小顺序排列并确保烧焦面朝下。文章详细阐述了解决方案的思路与步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
}
内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值