每次从最底部开始处理,如果不是最大值,则把最大值翻到底部。这就是最优解。原理自己模拟一下就好。。。

注意半径不是从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的更多相关文章

  1. Uva 120 - Stacks of Flapjacks(构造法)

    UVA - 120  Stacks of Flapjacks Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld &a ...

  2. uva 120 stacks of flapjacks ——yhx

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  3. (白书训练计划)UVa 120 Stacks of Flapjacks(构造法)

    题目地址:UVa 120 水题. 从最大的開始移,每次都把大的先翻到最上面,再翻到以下. 代码例如以下: #include <iostream> #include <cstdio&g ...

  4. UVa 120 Stacks of Flapjacks【构造法】

    题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...

  5. UVA - 120 Stacks of Flapjacks(煎饼)

    题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数k,把从锅底开始数第k张以及其上面的煎饼全部翻过来,最终使煎饼有序排列(锅顶最小,锅底最大). 分析:依次从锅底向上,优先排数字最大的煎饼.每次 ...

  6. UVaOJ 120 - Stacks of Flapjacks

    120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...

  7. uva Stacks of Flapjacks

                                                     Stacks of Flapjacks  题目链接:Click Here~ 题目描写叙述:     ...

  8. 【思维】Stacks of Flapjacks

    [UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...

  9. Stacks of Flapjacks(栈)

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

随机推荐

  1. java 数字补齐0

    String str_f = str.substring(0, 1); int i = (Integer.parseInt(str.substring(1)) + 1); // 数字补齐0 Decim ...

  2. tp中u方法的使用

    自学的时候都没怎么使用过该方法,现在刚进入一个新公司参加项目.发现这个方法用的很多,所以记录下来防止以后忘了. U方法用于完成对URL地址的组装,特点在于可以自动根据当前的URL模式和设置生成对应的U ...

  3. HDU 4939 Stupid Tower Defense

    dp:枚举red,dp前i 个塔中有j 个蓝塔的最大伤害. 机智的地方:dp前i 个塔的时候可以同时处理n-i 个红塔,这样就少了个循环...(枚举红塔的循环) #include <iostre ...

  4. knockout.js简单实用教程1

    第一次接触knockout是在一年多之前吧.当时是接手了一个别人的项目,在项目中有用到knockout来进行数据的绑定.也就开始学习起来knockout.在之后的项目中也多次用到了这个.在第一次开始学 ...

  5. mongodb操作记录

    [User]1.db.addUser("name","pwd","true/false")2.db.auth("name" ...

  6. Ext4 MVC CRUD操作

    项目目录结构如下: (1)index.html <!DOCTYPE html> <html> <head> <title>用户管理</title& ...

  7. 【转】Android下编译jni库的二种方法(含示例) -- 不错

    原文网址:http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html 总结如下:两种方法是:1)使用Android源码中的Make系统2)使用NDK(从N ...

  8. Seletion Sort

    referrence: GeeksforGeeks The selection sort algorithm sorts an array by repeatedly finding the mini ...

  9. Binary Tree Level Order Traversal 解答

    Question Given a binary tree, return the level order traversal of its nodes' values. (ie, from left ...

  10. ios sourecTree

    1, http://www.jianshu.com/p/be9f0484af9d 2, http://blog.csdn.net/viewcode/article/details/42291973