【思维】Stacks of Flapjacks
[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的更多相关文章
- uva 120 stacks of flapjacks ——yhx
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- UVaOJ 120 - Stacks of Flapjacks
120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...
- Uva 120 - Stacks of Flapjacks(构造法)
UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- uva Stacks of Flapjacks
Stacks of Flapjacks 题目链接:Click Here~ 题目描写叙述: ...
- Stacks of Flapjacks(栈)
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- Stacks of Flapjacks
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data s ...
- UVa120 - Stacks of Flapjacks
Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...
- B - Stacks of Flapjacks UVA - 120
BackgroundStacks and Queues are often considered the bread and butter of data structures and find us ...
- UVa 120 Stacks of Flapjacks【构造法】
题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...
随机推荐
- Html5学习2(Html表格、Html列表、Html5新元素、Canvas (坐标、路径、画圆、文本、渐变、图像))
Html表格 1.表格中的表头:<th></th>.其中表头部分字体加粗,颜色深绿色 <h4>水平标题:</h4> <table border=& ...
- UIImageView与UIScrollView的关系图
UIImageView与UIScrollView的关系图 https://www.evernote.com/shard/s227/sh/0af9f23c-08e6-4be6 ...
- HDU 1069 Monkey and Banana (dp)
题目链接 Problem Description A group of researchers are designing an experiment to test the IQ of a monk ...
- macaca安装失败的解决办法!
https://github.com/macacajs/macaca-android https://www.jianshu.com/p/76a5be6c1036
- C基础 读写锁中级剖析
引言 读写锁 是为了 解决, 大量 ''读'' 和 少量 ''写'' 的业务而设计的. 读写锁有3个特征: 1.当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞 2.当 ...
- CSS原生布局方式
前言 网页原生布局的方法其实网上有很多,大概为Flow(流动布局模型).Float(浮动布局模型).Layer(层级布局模型).<!--more--> Flow布局 流动布局模型其实就是默 ...
- 微信小程序宽高适配
小程序的宽任何机型都是750rpx,但是画布canvas的默认单位是px,可能会出现需要怪异的样式,我们可以用到 wx.getSystemInfoSync().windowWidth和 wx.getS ...
- 在Dubbo中使用高效的Java序列化(Kryo和FST)
在Dubbo中使用高效的Java序列化(Kryo和FST) 作者:沈理 文档版权:Creative Commons 3.0许可证 署名-禁止演绎 完善中…… TODO 生成可点击的目录 目录 序列化漫 ...
- 【python】msgpack使用
1.存储数据 import msgpack var={'a':'this','b':'is','c':'a test'} with open('file.name','wb') as f: msgpa ...
- intellij idea 无法创建类文件,方法。