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 ...
随机推荐
- 通用表表达式(Common Table Expression)
问题:编写由基本的 SELECT/FROM/WHERE 类型的语句派生而来的复杂 SQL 语句. 方案1:编写在From子句内使用派生表(内联视图)的T-SQL查询语句. 方案2:使用视图 方案3:使 ...
- 虚拟桌面基础架构(VDI)与终端服务和传统PC对比
VDI(Virtual Desktop Infrastructure),即虚拟桌面基础架构,正迅速成为一个热门词汇,它将颠覆企业向终端用户交付应用的游戏规则.这篇专题就是想通过VDI与两种传统技术的对 ...
- POJ动态规划题目列表
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- SSH与SSL
1. SSL SSH 即Secure Shell,它主要由三部分组成: 第一部分:连接协议 [SSH-CONNECT] 将多个加密隧道分成逻辑通道.它运行在用户认证协议上.它提供了交互式登录话路.远程 ...
- Spring AOP + AspectJ in XML configuration example
For those don't like annotation or using JDK 1.4, you can use AspectJ in XML based instead. Review l ...
- poj 3180 The Cow Prom(强联通分量)
http://poj.org/problem?id=3180 The Cow Prom Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- hdu 1689 Just a Hook
http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Time Limit: 4000/2000 MS (Java/Others) ...
- 任务分发系统gearman
1 Gearman是什么 Gearman Job Server@http://gearman.org/. Gearman 是一个任务分发系统,它提供了一个分发框架,能够分发某类任务到更适合处理这类任务 ...
- mybatis generator配置文件
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration ...
- Spring @RequestHeader用法
Spring MVC提供了 @RequestHeader注解,能够将请求头中的变量值映射到控制器的参数中.下面是一个简单的例子: import org.springframework.stereoty ...