[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. 【Zigbee技术入门教程-02】一图读懂ZStack协议栈的核心思想与工作机理

    [Zigbee技术入门教程-02]一图读懂ZStack协议栈的核心思想与工作机理 广东职业技术学院  欧浩源   Z-Stack协议栈是一个基于任务轮询方式的操作系统,其任务调度和资源分配由操作系统抽 ...

  2. 【CC2530入门教程-02】CC2530的通用I/O端口输入和输出控制

    第2课  CC2530的通用I/O端口输入和输出控制 小蜜蜂科教 / 广东职业技术学院  欧浩源 [通用I/O端口视频教程:https://v.qq.com/x/page/x0793aol7us.ht ...

  3. bzoj 2165 DP

    首先如果不考虑数据范围的话,因为每一层都是等效的,所以我们可以用w[i][j][k]来表示在某一层的j位置,称作i次电梯到k位置,最多上升多少层,那么我们可以比较容易的写出转移,因为m十分大,i可能与 ...

  4. Python面向对象学习2(面向对象的语法和特性,待更新)

    上一个内容我们介绍了面向对象和面向对象场景现在我们来学习下语法和特性 1,面向对象基本语法: # -*- coding:utf-8 -*- # Author: Colin Yao class Dog( ...

  5. Spring Cloud与Spring Boot的关系

    1.Spring Cloud是一个工具集:Spring   Cloud是在Spring    Boot的基础上构建的,用于简化分布式系统构建的工具集:使架构师在创建和发布微服务时极为便捷和有效. Sp ...

  6. 如何修改或美化linux终端

    先丢一张效果图: 如何让您的 LD 的终端更具个性呢?首先,我们需要了解下面几点知识.A:配置文件 个人配置文件:~/.bashrc全局设定文件:/etc/bash.bashrc(修改需要管理员权限) ...

  7. Linux 内核同步之自旋锁与信号量的异同【转】

    转自:http://blog.csdn.net/liuxd3000/article/details/8567070 Linux 设备驱动中必须解决的一个问题是多个进程对共享资源的并发访问,并发访问会导 ...

  8. SourceTree 过期,注册导入许可证

    参考这里:SourceTree过期,需要注册导入 SourceTree License 许可证 很详细 补充: 如果在 SourceTree 软件里注册失败,可以在网页注册. 如果其他邮箱不支持,可以 ...

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

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

  10. 异步网络模块之aiohttp的使用(一)

    异步网络模块之aiohttp的使用(一) 平时我们也许用的更多的是requests模块,或者是requests_hml模块,但是他们都属于阻塞类型的不支持异步,速度很难提高,于是后来出现了异步的gre ...