[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. Bazinga(HDU5510+KMP)

    t题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题目: 题意:找到一个编号最大的字符串满足:存在一个编号比它小的字符串不是它的字串. 思路:K ...

  2. 父元素与子元素之间的margin-top问题(css hack)

    hack: 父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. hytml代码: &l ...

  3. Tomcat的安装以及基本配置

    Tomcat是目前最常见也是最流行的基于java的一个web服务器软件   Tomcat的安装   (1)首先需要java环境,也就是说要依赖于java虚拟机JVM   (2)下载Tomcat ,地址 ...

  4. 转载 JAVA SE 连接ACCESS

    本代码实现连接 本机数据库的方法. 操作步骤: 1.进入控制面板,打开“管理工具→数据源(ODBC)”,弹出“ODBC数据源管理器”,在“用户DSN”选项卡中,单击选中名称为“Visio Databa ...

  5. jq_$.extend和$.fn.extend插件开发和方法的封装

    --------杂谈-------- 随着使用的使用js和jq使用的越来越娴熟,也就是说日常产品经理给的一些需求,已经在自己的能力范围内了.空出来了一点时间,来研究如何优化你的代码, 不管是性能上,还 ...

  6. github删除文件夹

    git rm -rf dirgit add .git commit -m 'remove dir'git push origin master //dir是要删除的文件夹路径

  7. YUV颜色编码解析(转)

    原文转自 https://www.jianshu.com/p/a91502c00fb0

  8. windows下常用快捷键(转)

    原文转自 https://blog.csdn.net/LJFPHP/article/details/78818696 win+E                 打开文件管器 win+D        ...

  9. LCD 每隔10分钟 自动熄灭 --打开Framebuffer console的时候【转】

    转自:http://blog.csdn.net/liujia2100/article/details/9009063 版权声明:本文为博主原创文章,未经博主允许不得转载. 之前移植LCD的时候,一切正 ...

  10. http状态码+http请求方式

    一.http状态码 2开头 (请求成功)表示成功处理了请求的状态代码. 200   (成功)  服务器已成功处理了请求. 通常,这表示服务器提供了请求的网页. 201   (已创建)  请求成功并且服 ...