水水题。给出煎饼数列, 一次只能让第一个到第i个数列全部反转,要求把数列排序为升序。

算法点破后不值几钱。。。

只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了。递归或循环即可。

把大饼放后面只要先让前面翻转使大饼排在后面,再整体翻转让大饼排到后面。

这题不是求最优解,我也不知道这样是不是最优解。

AC代码:

#include <cstdio>
#include <cstring>
int const maxn = 31;
int arr[maxn], n; void rev(int cut) {
printf("%d ", n - cut);
for (int i = 0; i <= cut / 2; i++) {
int tmp = arr[i];
arr[i] = arr[cut - i];
arr[cut - i] = tmp;
}//for
}//rev int scan(void) {
char ch = 0;
memset(arr, 0, sizeof(arr));
if (scanf("%d", &arr[0]) == EOF) return 0;
int i = 1;
while ((ch = getchar()) && ch != '\n') {
scanf("%d", &arr[i++]);
}//while
return i;
}//get a array int main() {
freopen("in", "r", stdin);
while (n = scan()) {
printf("%d", arr[0]);
for (int i = 1; i < n; i++)
printf(" %d", arr[i]);
printf("\n");
int cnt = n;
while (cnt) {
int max = 0, rec;
for (int i = 0; i < cnt; i++) {
if (arr[i] > max) {
max = arr[i];
rec = i;
}//if
}//for choose the max
if (rec != cnt - 1) {
if (rec != 0)
rev(rec);
rev(cnt - 1);
}
cnt --;
}//while recycle
printf("0\n");
}//while
return 0;
}

复杂度应该是n^3,用时0.019s,看到人家0.000s的实在佩服。。。

Uva120 Stacks of Flapjacks 翻煎饼的更多相关文章

  1. UVa120 - Stacks of Flapjacks

    Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...

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

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

  3. uva120 Stacks of Flapjacks (构造法)

    这个题没什么算法,就是想出怎么把答案构造出来就行. 思路:越大的越放在底端,那么每次就找出还没搞定的最大的,把它移到当前还没定好的那些位置的最底端,定好的就不用管了. 这道题要处理好输入,每次输入的一 ...

  4. 【思维】Stacks of Flapjacks

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

  5. UVaOJ 120 - Stacks of Flapjacks

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

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

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

  7. uva 120 stacks of flapjacks ——yhx

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

  8. uva Stacks of Flapjacks

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

  9. Stacks of Flapjacks(栈)

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

随机推荐

  1. ubuntu下通过pip安装pyside

    首先安装相关库 sudo apt-get install build-essential git cmake libqt4-dev libphonon-dev python2.7-dev libxml ...

  2. STS中搭建SpringMVC工程

    1 环境说明 首次接触Spring,面对这么一个优秀的框架,先从环境搞起,再慢慢学.开发环境选择Spring Tool Suite,得专业点不是?Maven选2.2.1,JDK还是1.6,Tomcat ...

  3. Static块详解

    首先,我们先看一段程序,代码如下: public class Father { public Father() //构造方法 { System.out.println(" 父类构造方法&qu ...

  4. HDU ACM Eight

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 解题背景: 看到八数码问题,没有任何的想法,偶然在翻看以前做的题的时候发现解决过类似的一道题,不 ...

  5. 现代程序设计——homework-10

    设计 对于MVC我的理解是这样的,V是台显示器,注意仅仅是一台比显示器普通显示器多几个按钮,用户按什么,按了什么该干什么都不用操心:M是实体的软件抽象,假设实体可以但不执行,我就可以一步一步走,实体可 ...

  6. Com进程通信(Delphi2007)

    相关资料: 1.http://my.oschina.net/u/582827/blog/2847662.http://www.cnblogs.com/findumars/p/5277561.html3 ...

  7. Cognos报表打开参数

    查看门户页面 http://localhost:9300/p2pd/servlet/dispatch? b_action=xts.run &m=portal/cc.xts &gohom ...

  8. Web 前端最佳实践

    Web 最佳实践   前端   选择器 尽量使用ID选择器 基于Id的选择器:先使用ID选择器定位,然后再使用find方法精确查找   // Fast: $( "#container div ...

  9. CodeForces 706A Beru-taxi (数学计算,水题)

    题意:给定一个固定位置,和 n 个点及移动速度,问你这些点最快到固定点的时间. 析:一个一个的算距离,然后算时间. 代码如下: #pragma comment(linker, "/STACK ...

  10. Unity3D-Baked Lightmapping 示例学习

    首先,看一下摄像机的Rendering Paths http://game.ceeger.com/Manual/RenderingPaths.html 可以看出,对于灯光的渲染质量 Deferred ...