Stacks of Flapjacks 

Background

Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal languages.

This problem involves both butter and sustenance in the form of pancakes rather than bread in addition to a finicky server who flips pancakes according to a unique, but complete set of rules.

The Problem

Given a stack of pancakes, you are to write a program that indicates how the stack can be sorted so that the largest pancake is on the bottom and the smallest pancake is on the top. The size of a pancake is given by the pancake's diameter. All pancakes in a stack have different diameters.

Sorting a stack is done by a sequence of pancake ``flips''. A flip consists of inserting a spatula between two pancakes in a stack and flipping (reversing) the pancakes on the spatula (reversing the sub-stack). A flip is specified by giving the position of the pancake on the bottom of the sub-stack to be flipped (relative to the whole stack). The pancake on the bottom of the whole stack has position 1 and the pancake on the top of a stack of n pancakes has position n.

A stack is specified by giving the diameter of each pancake in the stack in the order in which the pancakes appear.

For example, consider the three stacks of pancakes below (in which pancake 8 is the top-most pancake of the left stack):

         8           7           2
4 6 5
6 4 8
7 8 4
5 5 6
2 2 7

The stack on the left can be transformed to the stack in the middle via flip(3). The middle stack can be transformed into the right stack via the command flip(1).

The Input

The input consists of a sequence of stacks of pancakes. Each stack will consist of between 1 and 30 pancakes and each pancake will have an integer diameter between 1 and 100. The input is terminated by end-of-file. Each stack is given as a single line of input with the top pancake on a stack appearing first on a line, the bottom pancake appearing last, and all pancakes separated by a space.

The Output

For each stack of pancakes, the output should echo the original stack on one line, followed by some sequence of flips that results in the stack of pancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. For each stack the sequence of flips should be terminated by a 0 (indicating no more flips necessary). Once a stack is sorted, no more flips should be made.

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[],b[],n;
bool bb;
int rd()
{
int i,j,k,xx;
char c;
if (scanf("%c",&c)==-) return ;
xx=c-'';
while (scanf("%c",&c)&&c!=' '&&c!='\n')
xx=xx*+c-'';
if (c=='\n') bb=;
return xx;
}
void flip(int x)
{
int c[],i;
printf("%d ",n-x+);
memcpy(c,a,sizeof(a));
for (i=;i<=x;i++)
a[i]=c[x-i+];
}
int main()
{
int i,j,k,p,q,x,y,z;
bb=;
n=;
while (x=rd())
{
while (bb)
{
a[++n]=x;
x=rd();
}
a[++n]=x;
printf("%d",a[]);
for (i=;i<=n;i++)
printf(" %d",a[i]);
printf("\n");
memcpy(b,a,sizeof(a));
sort(b+,b+n+);
for (i=n;i>;i--)
{
p=i;
while (a[p]!=b[i]) p--;
if (i==p) continue;
if (p>) flip(p);
flip(i);
}
printf("0\n");
bb=;
n=;
}
}

由于没要求求出最优解,所以只要找到一个解即可。

方法是每次找见第i大的元素,如果他不在应该在的位置,先把它翻到顶上(如果已经在顶上就不用了),再翻到它该去的位置。

因为每次翻动都不会影响之下的,所以之前排好的不会重新弄乱。

注意重复元素的处理。找的时候从底下找,可以减少无用的翻动。

uva 120 stacks of flapjacks ——yhx的更多相关文章

  1. Uva 120 - Stacks of Flapjacks(构造法)

    UVA - 120  Stacks of Flapjacks Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld &a ...

  2. (白书训练计划)UVa 120 Stacks of Flapjacks(构造法)

    题目地址:UVa 120 水题. 从最大的開始移,每次都把大的先翻到最上面,再翻到以下. 代码例如以下: #include <iostream> #include <cstdio&g ...

  3. UVa 120 Stacks of Flapjacks【构造法】

    题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...

  4. UVA 120 Stacks of Flapjacks

    每次从最底部开始处理,如果不是最大值,则把最大值翻到底部.这就是最优解.原理自己模拟一下就好... 注意半径不是从1开始.数据处理要仔细. #include <iostream> #inc ...

  5. UVA - 120 Stacks of Flapjacks(煎饼)

    题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数k,把从锅底开始数第k张以及其上面的煎饼全部翻过来,最终使煎饼有序排列(锅顶最小,锅底最大). 分析:依次从锅底向上,优先排数字最大的煎饼.每次 ...

  6. UVaOJ 120 - Stacks of Flapjacks

    120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...

  7. uva Stacks of Flapjacks

                                                     Stacks of Flapjacks  题目链接:Click Here~ 题目描写叙述:     ...

  8. 【思维】Stacks of Flapjacks

    [UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...

  9. Stacks of Flapjacks(栈)

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

随机推荐

  1. NYOJ:题目860 又见01背包

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860 方法一:不用滚动数组(方法二为用滚动数组,为方法一的简化) 动态规划分析:最少要拿总 ...

  2. EffectiveJava——接口优于抽象类

    Java程序设计语言提供两种机制,可以用来定义允许多个实现的类型:接口和抽象方法,这两者直接醉为明显的区别在于,抽象类允许某些方法的实现,但接口不允许,一个更为重要的区别在于,为了实现由抽象类定义的类 ...

  3. Verilog学习笔记设计和验证篇(三)...............同步有限状态机的指导原则

    因为大多数的FPGA内部的触发器数目相当多,又加上独热码状态机(one hot code machine)的译码逻辑最为简单,所以在FPGA实现状态机时,往往采用独热码状态机(即每个状态只有一个寄存器 ...

  4. Oracle锁表与解锁

    查看锁表语句:方法1:    select sess.sid,           sess.serial#,           lo.oracle_username,           lo.o ...

  5. PHP遍历目录四种方法

    学习SPL的时候,遇到了DirectoryIterator这个目录类,谢了一下遍历目录的方法.于是总结一下遍历目录的四种写法 如下: <?php /* * 方法一:利用SPL的目录类,这个很简单 ...

  6. .net学习总结

    .NET 学前入门 了解.Net能做什么 了解.NET,C#语言及其特点(分清.NET和C#的关系),对.Net学习有系统全面的认识. C#基础 变量,赋值运算符.数据类型转换等. 选择结构控制(if ...

  7. 为网站添加ico图标

    1.将目标图片装换为.ico格式 在线转换工具:http://www.bitbug.net/ 2.将装换后的图标放入项目文件中,一般命名为favicon 3.在项目文件的index页面中引入 < ...

  8. abap 选择屏幕事件AT SELECTION-SCREEN

    AT SELECTION-SCREEN (1).其实就像一个FORM,所以在这个事件里声明的变量都是局部变量. (2).根据SY-UCOMM这个系统变量可以判断用户的命令 (3).在这个事件里响应的是 ...

  9. 安卓开发--android library projects cannot be launched错误

    最新因为学习,问技术友要了几个源代码,导入源代码的时候无法进行真机或者虚拟机测试. 原因:android library projects cannot be launched 百度了一下,解决方法很 ...

  10. 【读书笔记】iOS-NSDate

    +dateWithTimeIntervalSinceNow:接受一个NSTimeInterval参数,该参数是一个双精度值,表示以秒为单位的时间间隔.通过该参数可以指定时间偏移的方式:对于将来的时间, ...