南邮 OJ 1398 Flipping Burned Pancakes

阅读: 评论:0

南邮 OJ 1398 Flipping Burned Pancakes

南邮 OJ 1398 Flipping Burned Pancakes

Flipping Burned Pancakes

时间限制(普通/Java) :  1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 28            测试通过 : 12 

比赛描述

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.




输入

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. 


输出

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. 


样例输入

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

样例输出

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

提示

undefined

题目来源

Greater New York Region 2007




/* WA1
#include<iostream>
#define MAX_N 31int a[MAX_N];
int op[MAX_N*3];
bool flag[MAX_N];
int flipNo,n;void flip(int k){int i,temp;bool tempFlag;op[flipNo++] = k;for(i=0;i<k;i++){flag[i] = !flag[i];}for(i=0;(i<<1)<k;i++){temp = a[i];a[i] = a[k-1-i];a[k-1-i] = temp;tempFlag = flag[i];flag[i] = flag[k-1-i];flag[k-1-i] = tempFlag;}
}void init(){int i;scanf("%d",&n);flipNo = 0;for(i=0;i<n;i++){scanf("%d",a+i);if(a[i]<0){flag[i] = 1;	//为1表示负a[i] = -a[i];}else{flag[i] = 0;}}
}void handle(int k){int i;for(i=0;i<k;i++){if(a[i] == k){break;}}if(i==k-1 && flag[i]==0){	//不需要处理return;}if(i==0){					// WA k!=1if(flag[0]==0){flip(1);}return;}i++;flip(i);if(flag[0]==0){flip(1);}flip(k);
}void print(){int i;printf("%d",flipNo);for(i=0;i<flipNo;i++){printf(" %d",op[i]);}printf("n");
}int main(){
//	freopen(&#","r",stdin);int cas,ca,i;scanf("%d",&cas);for(ca=1;ca<=cas;ca++){init();for(i=n;i>0;i--){handle(i);}printf("%d ",ca);print();}
}
*//*
// AC 15MS
#include<iostream>
#define MAX_N 35int a[MAX_N];
int op[MAX_N*3];
bool flag[MAX_N];
int flipNo,n;void flip(int k){int i,temp;bool tempFlag;op[flipNo++] = k;for(i=0;i<k;i++){flag[i] = !flag[i];}for(i=0;(i<<1)<k;i++){temp = a[i];a[i] = a[k-1-i];a[k-1-i] = temp;tempFlag = flag[i];flag[i] = flag[k-1-i];flag[k-1-i] = tempFlag;}
}void init(){int i;scanf("%d",&n);flipNo = 0;for(i=0;i<n;i++){scanf("%d",a+i);if(a[i]<0){flag[i] = 1;	//为1表示负a[i] = -a[i];}else{flag[i] = 0;}}
}void handle(int k){int i;if(a[k-1]==k && flag[k-1]==0){return;}while(1){for(i=0;i<k;i++){if(a[i] == k){break;}}if(i==0 && flag[i]==1){flip(k);return;}else{flip(i+1);}}
}void print(){int i;printf("%d",flipNo);for(i=0;i<flipNo;i++){printf(" %d",op[i]);}printf("n");
}int main(){freopen(&#","r",stdin);int cas,ca,i;scanf("%d",&cas);for(ca=1;ca<=cas;ca++){init();for(i=n;i>0;i--){handle(i);}printf("%d ",ca);print();}
}
*/// AC 15MS
#include<iostream>
#define MAX_N 31int a[MAX_N];
int op[MAX_N*3];
bool flag[MAX_N];
int flipNo,n;void flip(int k){int i,temp;bool tempFlag;op[flipNo++] = k;for(i=0;i<k;i++){flag[i] = !flag[i];}for(i=0;(i<<1)<k;i++){temp = a[i];a[i] = a[k-1-i];a[k-1-i] = temp;tempFlag = flag[i];flag[i] = flag[k-1-i];flag[k-1-i] = tempFlag;}
}void init(){int i;scanf("%d",&n);flipNo = 0;for(i=0;i<n;i++){scanf("%d",a+i);if(a[i]<0){flag[i] = 1;	//为1表示负a[i] = -a[i];}else{flag[i] = 0;}}
}void handle(int k){int i;for(i=0;i<k;i++){if(a[i] == k){break;}}if(i==k-1 && flag[i]==0){	//不需要处理return;}if(k==1){flip(1);return;}if(i){flip(i+1);}if(flag[0]==0){flip(1);}flip(k);
}void print(){int i;printf("%d",flipNo);for(i=0;i<flipNo;i++){printf(" %d",op[i]);}printf("n");
}int main(){
//	freopen(&#","r",stdin);int cas,ca,i;scanf("%d",&cas);for(ca=1;ca<=cas;ca++){init();for(i=n;i>0;i--){handle(i);}printf("%d ",ca);print();}
}






本文发布于:2024-01-29 06:44:38,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170648188113440.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:南邮   OJ   Pancakes   Burned   Flipping
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23