UVA 120 Stacks of Flapjacks
每次从最底部开始处理,如果不是最大值,则把最大值翻到底部。这就是最优解。原理自己模拟一下就好。。。
注意半径不是从1开始。数据处理要仔细。
- #include <iostream>
- #include <cstring>
- #include <algorithm>
- #include <cstdio>
- using namespace std;
- int main (){
- int a[],b[],c[];
- char str[];
- while (gets (str)!=NULL){
- cout<<str<<endl;
- int len;
- int cnt=;
- len=strlen (str);
- memset (a,,sizeof a);
- for (int i=;i<len;i++){
- while (str[i]!=' '&&i<len)
- a[cnt]=a[cnt]*+str[i++]-'';
- b[cnt]=a[cnt];
- cnt++;
- }
- cnt--;
- sort (b+,b++cnt);
- for (int i=;i<=cnt;i++)
- c[b[i]]=i;//cout<<a[i]<<" ";
- for (int i=;i<=cnt;i++)
- a[i]=c[a[i]];//cout<<b[i]<<"r ";cout<<endl;
- for (int i=cnt;i>=;i--){
- if (a[i]!=i){
- if (a[]==i){
- cout<<cnt-i+<<" ";
- for (int j=;j<i;j++){
- b[j]=a[i-j+];
- }
- for (int j=;j<i;j++)
- a[j]=b[j];
- }
- else{
- int temp;
- for (int j=;j<=cnt;j++){
- if (a[j]==i){
- temp=j;break ;
- }
- }//cout<<temp<<"err";
- int f=;
- for (int j=i;j>temp;j--)
- b[++f]=a[j];
- for (int j=;j<temp;j++)
- b[++f]=a[j];
- for (int j=;j<=f;j++)
- a[j]=b[j];
- cout<<cnt-temp+<<" "<<cnt-i+<<" ";
- }
- }
- }
- cout<<<<endl;
- }
- return ;
- }
UVA 120 Stacks of Flapjacks的更多相关文章
- 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 120 Stacks of Flapjacks(构造法)
题目地址:UVa 120 水题. 从最大的開始移,每次都把大的先翻到最上面,再翻到以下. 代码例如以下: #include <iostream> #include <cstdio&g ...
- UVa 120 Stacks of Flapjacks【构造法】
题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...
- UVA - 120 Stacks of Flapjacks(煎饼)
题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数k,把从锅底开始数第k张以及其上面的煎饼全部翻过来,最终使煎饼有序排列(锅顶最小,锅底最大). 分析:依次从锅底向上,优先排数字最大的煎饼.每次 ...
- UVaOJ 120 - Stacks of Flapjacks
120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...
- uva Stacks of Flapjacks
Stacks of Flapjacks 题目链接:Click Here~ 题目描写叙述: ...
- 【思维】Stacks of Flapjacks
[UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...
- Stacks of Flapjacks(栈)
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
随机推荐
- ecos编译redboot,vmware运行redboot,执行hello world(图形配置)
1- 下载源文件 :~/ecos$ ls -lrt total 80 drwxr-xr-x 7 XXXX XXXX 4096 Mar 27 2009 ecos-3.0 -rw-r--r-- 1 X ...
- 网易2014校园招聘杭州Java笔试题
10) ABC http://soft.chinabyte.com/os/56/12516056.shtml 11) BD. 12) AC. http://blog.sina.com.cn/s/blo ...
- error: ‘shared_ptr’ in namespace ‘std’ does not name a type
用G++编译项目的时候发生标题上的错误,原因是,这是c++ 11标准的.在给g++传递命令行的时候加上-std=c++0x就行了. 还需要在源码中#include<memory> 我的cm ...
- 【转】 linux内核移植和驱动添加(三)
原文网址:http://blog.chinaunix.net/uid-29589379-id-4708909.html 原文地址:linux内核移植和驱动添加(三) 作者:genehang 四,LED ...
- IA32与x86
64位机器相比于32位优点 ①访问虚拟地址范围更大 ②更多的寄存器 ③过程较简单 ④采用条件传送指令 详细看:http://baike.baidu.com/link?url=DoRp7iW_z3cE6 ...
- DFS(深度优先搜索)模板
void dfs()//参数用来表示状态 { if(到达终点状态) { ...//根据题意来添加 return; } if(越界或者是不符合法状态) return; for(扩展方式) { if(扩展 ...
- MyCat 安装部署,实现数据库分片存储
一.安装MySQL或MariaDB(本文以MariaDB为例) MySQL手动安装方法:点击查看 MariaDB安装: 1.下载MariaDB的repo $ vi /etc/yum.repos.d/M ...
- U-Boot 启动过程和源码分析(第二阶段)-main_loop分析
1> main_loop common/main.c /******************************************************************** ...
- Grails重定向以及Render、Session的使用
近期发现网上grails因为用的人不算非常多,相关的资料也不是那么easy找到,随着学习的深入打算把我的学习心得定期分享出来.也给学习grails的同胞一个偷懒的机会,我的原则是以最简洁的方式学会Gr ...
- Android窗口管理服务WindowManagerService的简要介绍和学习计划
在前一个系列文章中,我们从个体的角度来分析了Android应用程序窗口的实现框架.事实上,如果我们从整体的角度来看,Android应用程序窗口的 实现要更复杂,因为它们的类型和作用不同,且会相互影响. ...