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 ...
随机推荐
- PHP 获取客户端IP
function get_ip() { static $realIP; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_ ...
- Meteor Shower(POJ 3669)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12816 Accepted: 3451 De ...
- 使用ARM模板部署自动扩展的Linux VMSS(1)
在Azure之前的ASM版本或者经典模式中,用户使用Azure Website,Azure Cloud Service等PAAS服务,可以实现一定程度上的自动扩展(auto scaling),但有着诸 ...
- WPF制作的一个小功能,智能提示(IntelliSense)
原文http://www.cnblogs.com/scheshan/archive/2012/06/30/2570867.html 最近WPF项目中遇到一个需求,需要给一个RichTextBox添加智 ...
- 简单了解下Dubbo
1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...
- struct内存对齐
内存对齐其实是为了在程序运行的时候更快的查找内存而做的一种编译器优化. 我们先看这样一个例子: #include <iostream> using namespace std; struc ...
- PHP伪静态与短链接
如今,Web服务高速发展的时代,各式各类的门户网站,如新浪http://www.sina.com.腾讯http://www.qq.com,这些网站大家都很容易记住,因为这种名称都是有规则和含义的.如果 ...
- [Oracle] Data Guard 系列(5) - 创建逻辑备库
在创建逻辑备库之前,必须得先创建物理备库,关于如何创建物理备库,请参考<Data Guard 系列(4) - 在不停主库的情况下创建物理备库>. 1. 在物理备库上停止日志应用服务 SYS ...
- 04747_Java语言程序设计(一)_第1章_Java语言基础
二进制0b开头 八进制0开头 十六进制0x开头 package com.jacky; public class Aserver { public static void main(String arg ...
- eclipse中误删了servers文件
Eclipse中误删了servers文件,需要重新添加tomcat服务器,这时就会遇到在New Server对话框中选择了Tomcat 6/7后却无法单击"Next"按钮的问题,如 ...