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):

  1. 8 7 2
  2. 4 6 5
  3. 6 4 8
  4. 7 8 4
  5. 5 5 6
  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. 1 2 3 4 5
  2. 5 4 3 2 1
  3. 5 1 2 3 4

Sample Output

  1. 1 2 3 4 5
  2. 0
  3. 5 4 3 2 1
  4. 1 0
  5. 5 1 2 3 4
  6. 1 2 0
  7.  
  8. 题解:构造法,选择排序的思想
    把半径的大小与位置对应对应于每叠煎饼数据,
    必须在第一行输出原叠的内容,接下来输出一组翻转动作的序列,使得这一叠煎饼自底向上由大至小的排列,一一对应。输出的每一组翻转动作序列都要由0来结束。一旦一叠煎饼已经排好序,就不能再进行任何翻转
  9.  
  1. #include <stdio.h>
  2. int a[],b[],n,p,total,Max;
  3. void swap(int x)
  4. {
  5. int i, j, t;
  6. for(i=,j=x; j>i; j--,i++)
  7. {
  8. t = a[i];
  9. a[i] = a[j];
  10. a[j] = t;
  11. }
  12. }
  13. int main()
  14. {
  15. while(scanf("%d",&a[])==)
  16. {
  17. n = ;
  18. while()
  19. {
  20. if(getchar()!=' ')
  21. break;
  22. scanf("%d", &a[n]);
  23. n++;
  24. }
  25. printf("%d",a[]);
  26. for(int i=; i<n; i++)
  27. printf(" %d", a[i]);
  28. printf("\n");
  29. total=;
  30. for(int i=; i<n; i++)
  31. {
  32. Max=;
  33. for(int j=n-i-; j>=; j--)
  34. if(Max<a[j])
  35. {
  36. Max=a[j];
  37. p=j;
  38. }
  39. if(p!=n-i-)
  40. {
  41. if(p!=)
  42. {
  43. swap(p);
  44. b[total++]=n-p;
  45. }
  46. swap(n-i-);
  47. b[total++]=i+;
  48. }
  49. }
  50. if(total == )
  51. printf("%d\n",);
  52. else
  53. {
  54. for(int i=; i<total; i++)
  55. printf("%d ",b[i]);
  56. printf("%d\n", );
  57. }
  58. }
  59. return ;
  60. }

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. [Sequence Alignment Methods] Smith–Waterman algorithm

    Smith–Waterman algorithm 首先需要澄清一个事实,Smith–Waterman algorithm是求两个序列的最佳subsequence匹配,与之对应的算法但是求两个序列整体匹 ...

  2. Jenkins 十二: 集成 selenium 测试

    我的selenium采用的是python版本. 其实 selenium java版本也类似. 1. 在jenkins里面新建selenium 测试项目. 源码管理采用 “Subversion”,输入 ...

  3. Java 自动装箱和拆箱

    JDK1.5之后的功能 自动装箱:指的是开发人员可以把一个基本数据类型直接赋给对应的包装类 自动拆箱:指开发人员可以把一个包装类对象直接赋给对应的基本数据类型 public static void m ...

  4. .net core4

  5. Collections之sort、reverse

    在使用List集合时,通常情况下希望从集合中得到的对象是按照一定顺序排列的,但是List集合的默认排序方式为按照对象的插入顺序,可以通过java.util.Collections类的静态方法sort( ...

  6. css分割线 文字居中的7种实现方式

    最近开始研究前端,会不定期更新一些小块代码,写下自己的学习笔记,也希望能和大家一起进步! 1.单个标签实现分隔线: <html> <head lang="en"& ...

  7. App 启动加载广告页面思路

    需求 很多app(如淘宝.美团等)在启动图加载完毕后,还会显示几秒的广告,一般都有个跳过按钮可以跳过这个广告,有的app在点击广告页之后还会进入一个广告页面,点击返回进入首页.今天我们就来开发一个广告 ...

  8. .net验证控件

    一.客户端验证(用户体验,减少服务器端压力) 二.服务器端验证(防止恶意攻击,客户端js很容易被绕过) 验证控件:RequiredFieldValidator:字段必填:RangeValidator: ...

  9. 如何开通www国际域名个人网站

    欢迎访问我的域名http://www.baiqiantao.xyz 一.准备知识 什么是域名? 所有以www开头的域名,就是国际域名:不以www开头的域名,都是为子域名. 域名都有后缀,后缀是有意义的 ...

  10. mysql复习增删改查

    select * from torder where status='退款申请' UPDATE torder SET `status`='退款申请' WHERE status='等待付款' and i ...