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. 【Bootstrap基础学习】05 Bootstrap学习总结

    好吧,Copy了几天,这个总结算是把我对Bootstrap的一些理解写一下吧. Bootstrap只是一套别人写好的前端框架,直接拿来用就好. 不过对于专业的前端而言,如果不去把所有的代码都看一遍来理 ...

  2. SqlServer 2008 R2定时备份数据库,并且发送邮件通知

    先配置数据库的邮件设置,这样才可以发送邮件. 2. 3. 4. 5. 6. 7. 8. 9. 10. 总的预览图,如图 执行这一段(先发送备份邮件,然后进行数据备份,将昨天的发送数据插入到另一张表中, ...

  3. 容器---List和AbstractList

    一.前言 前面我们介绍了Collection及其抽象实现,在JAVA的容器体系里,由Collection派生出来的有两大体系,即List和Map.本文以及后续文章将重点分析List体系.本文将重点分析 ...

  4. sharepoint获取exchange邮箱报错:该帐户无权模拟所请求的用户

    现象: sharepoint获取exchange邮箱报错:该帐户无权模拟所请求的用户 处理办法: 1.Open the Exchange Management Shell 2.输入: New-Mana ...

  5. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q60-Q62)

    Question 60You have a SharePoint site collection that contains 100 subsites.You plan to create a Web ...

  6. 自定义带进度条的WebView , 增加获取web标题和url 回掉

    1.自定义ProgressWebView package com.app.android05; import android.content.Context; import android.graph ...

  7. 分形几何算法和实现(C语言)

    初识分形 1.分形的含义: 英文单词Fractal,它是由美籍法国数学家曼德勃罗(Benoit Mandelbrot)创造出来的.其含义是不规则的.破碎的.分数的.曼德勃罗是想用此词来描述自然界中传统 ...

  8. 安卓第六天笔记--ListView

    安卓第六天笔记--ListView 1.AdapteView AdapteView 继承ViewGroup它的本质是容器 AdapterView派生了3个子类: AbsListView AbsSpin ...

  9. Charles使用详情

    Charles各版本下载: Charles for Windows 32 bit Charles for Windows 64 bit Charles for linux Charles for Ma ...

  10. SharePreference是如何实现的——序列化XML文件

    还记得上一篇我们讲到了用SharePreference来存储数据,那么究竟它是如何实现的呢,今天我们就来仔细看看其实现的细节,我们给它一个准确的名字,叫做XML序列化器(XmlSerializer). ...