【思维】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张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...
随机推荐
- C#读取txt文件时中文乱码
解决办法 使用GB2312中文字符集 StreamReader reader = new StreamReader(txtUrl, Encoding.GetEncoding("gb2312& ...
- linux 下 /bin /sbin 的区别 -- (转)
/bin,/sbin,/usr/bin,/usr/sbin区别 / : this is root directory root 用户根目录 /bin : command ...
- 线程,JSP,Servlet面试题
线程编程方面 60.java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现Runna ...
- 安全测试===黑客攻击常用cmd命令大全
黑客常用命令大全net user heibai lovechina /add 加一个heibai的用户密码为lovechina net localgroup Administrators heibai ...
- 【模板】解决二分图匹配的强力算法——Hopcroft-Karp算法
详细解释 参见:http://blog.csdn.net/wall_f/article/details/8248373 简要过程 HK算法可以当成是匈牙利算法的优化版,和dinic算法的思想比较类似. ...
- Thinking in java基础之集合框架(转载)
集合简介(容器)把具有相同性质的一类东西,汇聚成一个整体,就可以称为集合,例如这里有20个苹果,我们把每一个苹果当成一个东西(一个对象),然后我们借用袋子把这20个苹果装起来,而这个袋子就是集合(也叫 ...
- AC日记——聪明的质监员 洛谷 P1314
聪明的质监员 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 #define l ...
- CentOS7.5***
一.借助谷歌上网助手 二.用ss来实现*** 下载工具 sudo yum install shadowsocks-libev 修改配置文件 sudo chmod 777 /etc/shadowsock ...
- CentOS 6.7下配置 yum 安装 Nginx
CentOS 6.7下配置 yum 安装 Nginx. 转载:http://www.linuxidc.com/Linux/2016-07/133283.htm 第一步,在/etc/yum.repos. ...
- 基于kbengine 0.4.20 解读
[以下文章转自kbe论坛] MMOG服务端是一种高品质的工程项目,品读开源的kbe是一种乐趣.本文档我带童鞋们一起领略一下.囿于我知识面和经验方面所限,文中所述之处难免有错误存在,还请读童鞋们睁大慧眼 ...