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 ...
随机推荐
- 内核源码分析之进程调度机制(基于3.16-rc4)
进程调度所使用到的数据结构: 1.就绪队列 内核为每一个cpu创建一个进程就绪队列,该队列上的进程均由该cpu执行,代码如下(kernel/sched/core.c). DEFINE_PER_CPU_ ...
- C# 字符串格式
1.数字补零输出字符串 String.Format("{0:D6}",123); 2.字符串靠左输出6位 String.Format("{0,-6}",&quo ...
- Spark RDD概念学习系列之RDD与DSM的异同分析(十三)
RDD是一种分布式的内存抽象,下表列出了RDD与分布式共享内存(Distributed Shared Memory,DSM)的对比. 在DSM系统[1]中,应用可以向全局地址空间的任意位置进行读写操作 ...
- Caroline--chochukmo
Caroline--chochukmo 虾米试听 Caroline, Caroline, Caroline, you pulled me into so deep down(内心深处). Caroli ...
- firefox 对相对定位的TD元素渲染错误
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=&quo ...
- JavaWeb应用中重定向与跳转的区别
针对JSP与Servlet: 两种重要的跳转: 1.request.getRequestDispatcher("地址").forward(ServletRequest, Servl ...
- 你应该知道的16个Linux服务器监控命令
在不同的Linux发行版中,会有不同的GUI程序可以显示各种系统信息,比如SUSE Linux发行版中,就有非常棒的图形化的配置和管理工具YaST,KDE桌面环境里的KDE System Guard也 ...
- hibernate[版本四]知识总结
1.hibernate是orm对象关系映射,是对jdbc的封装 2.hibernate版helloworld 2.1导入jar <dependencies> <dependency& ...
- System.getProperties()对应的key/value列表
Key Meaning ——————- —————————— "file.separator" File separa ...
- Hadoop平台配置总结
hadoop的配置,个人感觉是非常容易出问题.一个原因是要配置的地方多,还有个原因就是集群配置要在几台机器上都配置正确,才能保证配置好hadoop,跑起任务. 经过昨晚加今天上午的折腾,总算成功配好了 ...