[UVa120] Stacks of Flapjacks

算法入门经典第8章8-1 (P236)

题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列。

试题分析:从插入排序即可找到思路。每次我们优先地将没有到自己位置上的、最大的数挪到自己的位置上。

     为什么可以这样做呢?难道不会改变已经排好序的么。

     不会,因为我们从大往小处理,翻转的是前面的一段,而排好序的是后面一段,因此肯定不会打乱后面的。

     对于每一个数,设其下标为pos,已经排好序的有x个,那么我们先将pos其变为1,即翻转[1,pos],然后将其翻转到N-x那里去,所以再翻转[1,N-x]。

代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; #define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int INF=9999999;
const int MAXN=100000; int a[MAXN+1];
int cnt; int N,M;
char str[MAXN+1];
int b[MAXN+1]; bool cmp(int a,int b){
return a>b;
}
int ret;
void ref(int k){
for(int i=1;i<=k/2;i++)
swap(a[i],a[k-i+1]);
return ;
}
int ans[MAXN+1]; int main(){
while(scanf("%d",&a[1])!=EOF){
ret=0;
gets(str); cnt=1;
int len=strlen(str);
for(int i=0;i<len;i++){
if(str[i]==' ') continue;
else {
int p=i+1;
int sum=str[i]-'0';
while(isdigit(str[p])){
sum=sum*10+str[p]-'0';
++p;
}
i=p;
a[++cnt]=sum;
}
}
N=cnt;
for(int i=1;i<=N;i++) b[i]=a[i];
for(int i=1;i<N;i++) printf("%d ",b[i]);
printf("%d\n",b[N]);
sort(b+1,b+N+1,cmp);
for(int i=1;i<=N;i++){
int tmp;
for(int j=1;j<=N;j++){
if(a[j]==b[i]) {
tmp=j; break;
}
}
if(tmp==b[i]) continue;
if(tmp!=1){
ans[++ret]=N-tmp+1;
ref(tmp);
}
if(b[i]!=1){
ans[++ret]=i;
ref(N-i+1);
}
}
for(int i=1;i<=ret;i++) printf("%d ",ans[i]);
puts("0");
}
return 0;
}

  

【思维】Stacks of Flapjacks的更多相关文章

  1. uva 120 stacks of flapjacks ——yhx

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

  2. UVaOJ 120 - Stacks of Flapjacks

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

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

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

  4. uva Stacks of Flapjacks

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

  5. Stacks of Flapjacks(栈)

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

  6. Stacks of Flapjacks

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

  7. UVa120 - Stacks of Flapjacks

    Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...

  8. B - Stacks of Flapjacks UVA - 120

    BackgroundStacks and Queues are often considered the bread and butter of data structures and find us ...

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

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

随机推荐

  1. NSObject class和NSObject protocol的关系(抽象基类与协议)

    [转载请注明出处] 1.接口的实现 对于接口这一概念的支持,不同语言的实现形式不同.Java中,由于不支持多重继承,因此提供了一个Interface关键词.而在C++中,通常是通过定义抽象基类的方式来 ...

  2. 人人都能掌握的Java服务端性能优化方案

    作为一个Java后端开发,我们写出的大部分代码都决定着用户的使用体验.如果我们的后端代码性能不好,那么用户在访问我们的网站时就要浪费一些时间等待服务器的响应.这就可能导致用户投诉甚至用户的流失. 关于 ...

  3. Tunnel Warfare(HDU1540+线段树+区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目: 题意:总共有n个村庄,有q次操作,每次操作分为摧毁一座村庄,修复一座村庄,和查询与询问的 ...

  4. C语言分割字符串函数strtok

    在编程过程中,有时需要对字符串进行分割.而有效使用这些字符串分隔函数将会给我们带来很多的便利. 下面我将在MSDN中学到的strtok函数做如下翻译. strtok :在一个字符串查找下一个符号 ch ...

  5. pinctrl框架【转】

    转自:http://www.cnblogs.com/kevinhwang/p/5703192.html pinctrl框架是linux系统为统一各SOC厂家pin管理,目的是为了减少SOC厂家系统移植 ...

  6. Yii 1.1.17 五、分页类、关联模型、权限验证与默认页面跳转

    一.分页类使用 1.在控制器中 // 实例化 $criteria = new CDbCriteria(); $articleModel = Article::model(); // 分页 $total ...

  7. centos 引导盘

    # grub.conf generated by anaconda## Note that you do not have to rerun grub after making changes to ...

  8. Java GC策略

    本文转自https://blog.csdn.net/rabbit_in_android/article/details/50386954 内存管理和垃圾回收 JVM内存组成结构 JVM栈由堆.栈.本地 ...

  9. pycaffe使用.solverstate文件继续训练

    import caffe solver_file = "solver.prototxt" solverstate = "xx.solverstate" caff ...

  10. 如何在阿里云esc上安装wordpress

    本人的个人网纱建好,但是由于新申请的域名还没有备案 所以暂时无法通过域名访问. 1.按顺序边看边跟着做基本上你能够很优雅的完成大部分设置. http://bbs.aliyun.com/read/162 ...