HDU 1988 flipping burned pancakes (same problem as UVA pancake)

Source: Internet
Author: User

Problem DescriptionThe 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's bad business to serve visibly burned pancakes to the patrons. Before serving, the waitress would arrange the stacks of pancakes so, the burned sides is 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, and each of the which is burned on one side. The problem is to convert the stack to one in which the pancakes be in size order with the smallest on the top and the LA Rgest on the bottom and burned side to each pancake. To does this, we is
Allowed to flip the top K pancakes through as a unit (so the k-th pancake are now on top and the pancake previously on top are Now on the k-th position and the burned side goes from top to bottom and vice versa).
For example (+ indicates burned bottom,-a burned top):



Must write a program which finds a sequence of in most (3n–2) flips, which converts a given stacks of pancakes to a s orted stack with burned sides down.

Inputthe 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 datasets as a sequence of numbers separated by spaces. The first number on each line gives the number, M, and 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'll is, at the most, 30.

Outputfor each dataset, your 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 SEQ Uence of K numbers, each of which gives the number of pancakes to flip on the corresponding sorting step. There may is several correct solutions for some datasets. For instance 3 2 3 are also a solution to the first problem below.

Sample Input
33 +1-3-24-3 +1-2-45 +1 +2 +3 +4-5

Sample Output
1 6 2 1 3 1 2 12 6 4 1 4 3 1 23 3 5 1 5

Test instructions: N Pancake, sizes 1 to N, stacked from top to bottom. The face up is +, the opposite side upward is-. Each time you can select a number of blocks above the entire flip over. The step of the flip, can make the final face upward, the size from top to bottom is small to large.

Mark: There's no difficulty in picking the biggest one by not more than 3 times to get to the bottom. Sample is very good, the main thing is to pay attention to the top 1 of the processing on it.

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath>using namespace std; int a[50],ans[50];void sswap (int x) {int i,j;for (i=1,j=x;i<=x/2;i++,j--) {int temp=a[i];a[i]=a[j];a[j]=temp;} for (i=1;i<=x;i++) a[i]=-a[i];} int main () {int i,j,t,n;cin>>t;for (int _=1;_<=t;_++) {cin>>n;for (i=1;i<=n;i++) cin>>a[i];int K=0;for (i=n;i>=1;i--) {if (a[i]!=i) {if (j=1;j<i;j++) {if (Fabs (A[j]) ==i) break;} if (j!=1) {Sswap (j); ans[k++]=j;} if (a[1]>0) {sswap (1); ans[k++]=1;} sswap (i); ans[k++]=i;}} printf ("%d%d", _,k), for (i=0;i<k;i++) printf ("%d", Ans[i]);p rintf ("\ n");} return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1988 flipping burned pancakes (same problem as UVA pancake)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.