UVA - 120  Stacks of Flapjacks

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Description

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 commandflip(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.

Sample Input

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

Sample Output

1 2 3 4 5
0
5 4 3 2 1
1 0
5 1 2 3 4
1 2 0 题解:构造法,选择排序的思想
把半径的大小与位置对应对应于每叠煎饼数据,
必须在第一行输出原叠的内容,接下来输出一组翻转动作的序列,使得这一叠煎饼自底向上由大至小的排列,一一对应。输出的每一组翻转动作序列都要由0来结束。一旦一叠煎饼已经排好序,就不能再进行任何翻转
#include <stdio.h>
int a[],b[],n,p,total,Max;
void swap(int x)
{
int i, j, t;
for(i=,j=x; j>i; j--,i++)
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int main()
{
while(scanf("%d",&a[])==)
{
n = ;
while()
{
if(getchar()!=' ')
break;
scanf("%d", &a[n]);
n++;
}
printf("%d",a[]);
for(int i=; i<n; i++)
printf(" %d", a[i]);
printf("\n");
total=;
for(int i=; i<n; i++)
{
Max=;
for(int j=n-i-; j>=; j--)
if(Max<a[j])
{
Max=a[j];
p=j;
}
if(p!=n-i-)
{
if(p!=)
{
swap(p);
b[total++]=n-p;
}
swap(n-i-);
b[total++]=i+;
}
}
if(total == )
printf("%d\n",);
else
{
for(int i=; i<total; i++)
printf("%d ",b[i]);
printf("%d\n", );
}
}
return ;
}

Uva 120 - Stacks of Flapjacks(构造法)的更多相关文章

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

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

  2. uva 120 stacks of flapjacks ——yhx

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

  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 11925 Generating Permutations (构造法)

    题意:给定一个序列,让你从一个升序列变成该序列,并且只有两种操作,操作1:交换前两个元素,操作2:把第一个元素移动到最后. 析:一开始的时候吧,不会,还是看的题解,首先是要逆序来做,这样可能好做一点, ...

  8. uva Stacks of Flapjacks

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

  9. 紫书 习题 8-24 UVa 10366 (构造法)

    又是一道非常复杂的构造法-- #include<cstdio> #include<algorithm> #define REP(i, a, b) for(int i = (a) ...

随机推荐

  1. [Locked] Largest BST Subtree

    Largest BST Subtree Given a binary tree, find the largest subtree which is a Binary Search Tree (BST ...

  2. 高效的DDoS攻击探测与分析工具——FastNetMon

    一.简介 FastNetMon这是一个基于多种抓包引擎(NetFlow, IPFIX, sFLOW, netmap, PF_RING, PCAP)的DoS/DDoS攻击高效分析工具,可以探测和分析网络 ...

  3. 利用spring AOP 实现统一校验

    开发环境 JDK: 1.7 spring: 4.0.6 aspect: 1.7.4 应用背景   在APP与后台通讯的过程中,我们一般都会有个authToken的字符串校验,判断那些请求是需要校验用户 ...

  4. jni java和C之间的值传递(int String int[])

    我们通过jni调用C代码不可能每次只是去调一个方法,通常,我们需要传递一些值过去. 例如,播放电影,那就肯定需要你把电影的 url给 C的播放器吧,等等. 接下来就看一看怎么去传递这些值: 首先是最简 ...

  5. Protocol buffer序列化及其在微信蓝牙协议中的应用

    Protocol buffer是Google出品的一种轻便高效的结构化数据存储格式,可对结构化数据进行序列化,并具有语言无关.平台无关等特点,在通信协议和数据存储等领域已经得到广泛的应用.目前其已经提 ...

  6. 《Android开发艺术探索》读书笔记 (9) 第9章 四大组件的工作过程

    第9章 四大组件的工作过程 9.1 四大组件的运行状态 (1)四大组件中只有BroadcastReceiver既可以在AndroidManifest文件中注册,也可以在代码中注册,其他三个组件都必须在 ...

  7. ACM vim配置

    ACM现场赛时用的,比较简短,但是主要的功能都有了. 直接打开终端输入gedit ~/.vimrc 把下面的东西复制到里面就行了. filetype plugin indent on colo eve ...

  8. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  9. Day4 - Python基础4 迭代器、装饰器、软件开发规范

    Python之路,Day4 - Python基础4 (new版)   本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...

  10. contains选择器

    有这样一个问题: 一个列表里面,很多option,但是在不知道value,只知道他的内容的时候,怎么进行选择,比如: 北京市天津市上海市重庆市 在不知道他的value和index的时候,选择北京市,能 ...