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 ...
随机推荐
- POJ No.3680 Intervals
2016-06-01 22:01:39 题目链接: POJ No.3680 Intervals 题目大意: 给定N个带权区间,最多可以重复选一个点M次,求出一种选法使得所得权最大 解法: 费用流 建模 ...
- homework-08-作业2
1. 了解Lambda的用法 计算“Hello World!”中 a.字母‘e’的个数 b. 字母‘l’的个数 代码: void calcEL() { char s[100] = "Hell ...
- 在Android4.0中Contacts拨号盘界面剖析(源码)
通过在 ViewPager 的适配器对象中,发现过一下三行代码 private DialpadFragment mDialpadFragment; private CallLogFragment ...
- C#学习笔记(十四):GC机制和弱引用
垃圾回收(GC) 垃圾回收即Garbage Collector,垃圾指的是内存中已经不会再使用的对象,通过收集释放掉这些对象占用的内存. GC以应用程序的root为基础,遍历应用程序在Heap上动态分 ...
- VS2012与NUnit
微软提供的NUnit插件是针对vs2010的,而vs2012会自动识别,测试环境为64位win7,具体操作步骤如下 1.下载安装NUnit(NUnit-2.6.3.msi) 2.新建测试项目UnitT ...
- PostgreSQL中,database,schema,table之间关系
从逻辑上看,schema,table,都是位于database之下. 首先,在postgres数据库下建立表(相当于建立在public schema下): [pgsql@localhost bin]$ ...
- 删除链表中全部值为k的节点
1. 问题描写叙述 给定一个单链表,删除当中值为k的全部节点.比如:1→2→6→3→4→5→61 \to 2 \to 6 \to 3 \to 4 \to 5 \to 6,删除当中值为6的节点,返回:1 ...
- some scrum screenshots
backlog tracking progress Initial Stage Next Stage End
- C#操作Word (1)Word对象模型
Word对象模型 (.Net Perspective) 本文主要针对在Visual Studio中使用C# 开发关于Word的应用程序 来源:Understandingthe Word Object ...
- delphi TPopupMenu.Popup
procedure TPopupMenu.Popup(X, Y: Integer); 这个点是相对桌面的而不是窗体的 GetCursorPos是鼠标的位置 鼠标动这个点就不一样 v ...