Uva120 Stacks of Flapjacks 翻煎饼
水水题。给出煎饼数列, 一次只能让第一个到第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 翻煎饼的更多相关文章
- UVa120 - Stacks of Flapjacks
Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...
- UVA - 120 Stacks of Flapjacks(煎饼)
题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数k,把从锅底开始数第k张以及其上面的煎饼全部翻过来,最终使煎饼有序排列(锅顶最小,锅底最大). 分析:依次从锅底向上,优先排数字最大的煎饼.每次 ...
- uva120 Stacks of Flapjacks (构造法)
这个题没什么算法,就是想出怎么把答案构造出来就行. 思路:越大的越放在底端,那么每次就找出还没搞定的最大的,把它移到当前还没定好的那些位置的最底端,定好的就不用管了. 这道题要处理好输入,每次输入的一 ...
- 【思维】Stacks of Flapjacks
[UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...
- UVaOJ 120 - Stacks of Flapjacks
120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...
- Uva 120 - Stacks of Flapjacks(构造法)
UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- uva 120 stacks of flapjacks ——yhx
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- uva Stacks of Flapjacks
Stacks of Flapjacks 题目链接:Click Here~ 题目描写叙述: ...
- Stacks of Flapjacks(栈)
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
随机推荐
- MapReduce的执行过程.
作业在运行时,数据或者是作业调用的一个运行图. 用户写的代码通过JobClient提交给JobTracker Job对象中封装了JobClient JobConf和我们的Job对象几乎是一回事. 把我 ...
- 让sublime text 2更好地支持Python
SublimeCodeIntel: ~/.codeintel/config里加了python和pythonExtraPaths的路径(Mac): {"Python" : {&quo ...
- UVALive 7457 Discrete Logarithm Problem (暴力枚举)
Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...
- Spring入门(1)-第一个Spring项目
1. 创建maven项目,maven相关配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= ...
- 通用FASTREPORT打印模块及接口方法
untFastReport.dfm文件: object frmFastReport: TfrmFastReport OldCreateOrder = False Height = 405 Width ...
- Random的nextInt用法
因为想当然的认为Random类中nextInt()(注:不带参数),会产生伪随机的正整数,采用如下的方式生成0~99之间的随机数: Random random = new Random(); rand ...
- linux 下安装apache 快速教程
最近自学linux,看鸟哥的文章.提到了apache,所以在虚拟机redhat 5下安装了一把, 结合国内外文章写下快速可行的教程: --------------------------------- ...
- windows下实现微秒级的延时
windowsintegeriostream汇编嵌入式任务 最近正在做一个嵌入式系统,是基于windows ce的,外接硬件的时序要微秒级的延时.1.微秒级的延时肯定不能基于消息(SetTimer函数 ...
- mysql5.5主从配置
mysql主从同步# 一:mysql数据库的主从 mysql数据库5.5之后的版本和5.5以前的版本数据库主从存在差异,这里是针对数据库5.5之后的配置. 1.主库编辑my.cnf(linux的my. ...
- PHP 支持IMAP
linux下安装php支持imap 2008-11-26 10:31:10| 分类: linux 学习日志|举报|字号 订阅 第一步:安装apache注:当前目录为/tmp,目录下有http ...