每次从最底部开始处理,如果不是最大值,则把最大值翻到底部。这就是最优解。原理自己模拟一下就好。。。

注意半径不是从1开始。数据处理要仔细。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <cstdio>
  5. using namespace std;
  6.  
  7. int main (){
  8. int a[],b[],c[];
  9. char str[];
  10. while (gets (str)!=NULL){
  11. cout<<str<<endl;
  12. int len;
  13. int cnt=;
  14. len=strlen (str);
  15. memset (a,,sizeof a);
  16. for (int i=;i<len;i++){
  17. while (str[i]!=' '&&i<len)
  18. a[cnt]=a[cnt]*+str[i++]-'';
  19. b[cnt]=a[cnt];
  20. cnt++;
  21. }
  22. cnt--;
  23. sort (b+,b++cnt);
  24. for (int i=;i<=cnt;i++)
  25. c[b[i]]=i;//cout<<a[i]<<" ";
  26. for (int i=;i<=cnt;i++)
  27. a[i]=c[a[i]];//cout<<b[i]<<"r ";cout<<endl;
  28. for (int i=cnt;i>=;i--){
  29. if (a[i]!=i){
  30. if (a[]==i){
  31. cout<<cnt-i+<<" ";
  32. for (int j=;j<i;j++){
  33. b[j]=a[i-j+];
  34. }
  35. for (int j=;j<i;j++)
  36. a[j]=b[j];
  37. }
  38. else{
  39. int temp;
  40. for (int j=;j<=cnt;j++){
  41. if (a[j]==i){
  42. temp=j;break ;
  43. }
  44. }//cout<<temp<<"err";
  45. int f=;
  46. for (int j=i;j>temp;j--)
  47. b[++f]=a[j];
  48. for (int j=;j<temp;j++)
  49. b[++f]=a[j];
  50. for (int j=;j<=f;j++)
  51. a[j]=b[j];
  52. cout<<cnt-temp+<<" "<<cnt-i+<<" ";
  53. }
  54. }
  55. }
  56. cout<<<<endl;
  57. }
  58. return ;
  59. }

UVA 120 Stacks of Flapjacks的更多相关文章

  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 ——yhx

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

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

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

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

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

  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. ecos编译redboot,vmware运行redboot,执行hello world(图形配置)

    1- 下载源文件 :~/ecos$ ls -lrt total 80 drwxr-xr-x 7 XXXX XXXX  4096 Mar 27  2009 ecos-3.0 -rw-r--r-- 1 X ...

  2. 网易2014校园招聘杭州Java笔试题

    10) ABC http://soft.chinabyte.com/os/56/12516056.shtml 11) BD. 12) AC. http://blog.sina.com.cn/s/blo ...

  3. error: ‘shared_ptr’ in namespace ‘std’ does not name a type

    用G++编译项目的时候发生标题上的错误,原因是,这是c++ 11标准的.在给g++传递命令行的时候加上-std=c++0x就行了. 还需要在源码中#include<memory> 我的cm ...

  4. 【转】 linux内核移植和驱动添加(三)

    原文网址:http://blog.chinaunix.net/uid-29589379-id-4708909.html 原文地址:linux内核移植和驱动添加(三) 作者:genehang 四,LED ...

  5. IA32与x86

    64位机器相比于32位优点 ①访问虚拟地址范围更大 ②更多的寄存器 ③过程较简单 ④采用条件传送指令 详细看:http://baike.baidu.com/link?url=DoRp7iW_z3cE6 ...

  6. DFS(深度优先搜索)模板

    void dfs()//参数用来表示状态 { if(到达终点状态) { ...//根据题意来添加 return; } if(越界或者是不符合法状态) return; for(扩展方式) { if(扩展 ...

  7. MyCat 安装部署,实现数据库分片存储

    一.安装MySQL或MariaDB(本文以MariaDB为例) MySQL手动安装方法:点击查看 MariaDB安装: 1.下载MariaDB的repo $ vi /etc/yum.repos.d/M ...

  8. U-Boot 启动过程和源码分析(第二阶段)-main_loop分析

    1> main_loop  common/main.c /******************************************************************** ...

  9. Grails重定向以及Render、Session的使用

    近期发现网上grails因为用的人不算非常多,相关的资料也不是那么easy找到,随着学习的深入打算把我的学习心得定期分享出来.也给学习grails的同胞一个偷懒的机会,我的原则是以最简洁的方式学会Gr ...

  10. Android窗口管理服务WindowManagerService的简要介绍和学习计划

    在前一个系列文章中,我们从个体的角度来分析了Android应用程序窗口的实现框架.事实上,如果我们从整体的角度来看,Android应用程序窗口的 实现要更复杂,因为它们的类型和作用不同,且会相互影响. ...