8-1 Stacks of Flapjacks UVA120
题意: 有一叠煎饼在锅里 n n<=30张 每张都有一个数字 代表它的大小 厨师每次可以选择一个数k 把从锅底开始数第k张上面的煎饼全部反过来 即原来的在上面的煎饼现在到了下面 如
Sample Input
1 2 3 4 5
5 4 3 2 1
5 1 2 3 4
Sample Output
1 2 3 4 5
0
5 4 3 2 1
1 0
5 1 2 3 4
1 2 0
按照选择排序模拟
各种加一减一模拟的好乱 糟糕的代码
#include<bits/stdc++.h>
using namespace std;
#define N 30
int a[N];
int path[N];
int n,c; void chang(int x)
{
int temp[N];
for(int i=;i<=n-x+;i++)
temp[i]=a[n-x-i+];
for(int i=n-x+;i<=n;i++)
temp[i]=a[i];
memcpy(a,temp,sizeof a);
} void dfs(int cur)
{
if(cur==)
{
for(int i=;i<c;i++)
printf("%d ",path[i]);
printf("0\n");
return ;
}
int maxx=;
int u=;
for(int i=;i<=cur;i++)
{
if(a[i]>maxx){maxx=a[i],u=i; }
}
if(u!=cur)
{
if(u!=)
chang(n-u+);path[c++]=n-u+;
chang(n-cur+);path[c++]=n-cur+;
}
dfs(cur-);
} int main()
{
char s[];
while(fgets(s,,stdin))
{
int cnt=;
int v=;
int i=;
while(i<strlen(s))
{
while(!isspace(s[i])&&i<strlen(s))v=v*+s[i++]-'';
a[cnt++]=v;
v=;
i++;
} n=cnt-;
for(int i=;i<=n;i++)
{ printf("%d",a[i]);
if(i!=cnt)printf(" ");
}
printf("\n");
c=;
dfs(n);
}
}
数据读入用流来写很快!!! 还有找最大值下标函数!! 交换的话直接一个循环 加swap函数 不用temp
我写了70行 lrj大大写了30行
菜鸡还需努力。。。
#include<bits/stdc++.h>
using namespace std;
const int maxn = + ;
int n, a[maxn]; // 翻转a[0..p]
void flip(int p) {
for(int i = ; i < p-i; i++)
swap(a[i], a[p-i]);
printf("%d ", n-p);
} int main() {
string s;
while(getline(cin, s)) {
cout << s << "\n";
stringstream ss(s);
n = ;
while(ss >> a[n]) n++;
for(int i = n-; i > ; i--) {
int p = max_element(a, a+i+) - a; // 元素a[0..i]中的最大元素
if(p == i) continue;
if(p > ) flip(p); // flip(0)没啥意思,是不?
flip(i);
}
printf("0\n");
}
return ;
}
8-1 Stacks of Flapjacks UVA120的更多相关文章
- 【思维】Stacks of Flapjacks
[UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...
- 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 ...
- Uva120 Stacks of Flapjacks 翻煎饼
水水题.给出煎饼数列, 一次只能让第一个到第i个数列全部反转,要求把数列排序为升序. 算法点破后不值几钱... 只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了.递归或循环即 ...
随机推荐
- linux缓存手动清理
一般情况下不建议这么做, 如果你确定向的话还是可以的首先运行sync把未存盘的cache都写入磁盘,稍等片刻, 或者是直接运行sync 两遍 然后echo 1 试试应该大部分缓存可以释放 释放ca ...
- ASP.NET Core的身份认证框架IdentityServer4--(5)自定义用户登录(使用官网提供的UI)
IdentityServer官方提供web页面,可以根据需求修改样式.具体UI下载跟配置参考官网文档. 文档地址:https://identityserver4.readthedocs.io/en/r ...
- git 分支管理——多人协作
git 分支管理--多人协作 一般一个项目有一个master主分支,还有一个develop开发分支.主要是在develop分支上协作开发,然后merge合并到master主分支上. 当从远程仓库克隆时 ...
- bzoj千题计划148:bzoj1537: [POI2005]Aut- The Bus
http://www.lydsy.com/JudgeOnline/problem.php?id=1537 朴素的转移:dp[i][j]=max(dp[i][j-1],dp[i-1][j])+p[i][ ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
- 【转】WPF的知识
[-] 闲话WPF之二XAML概述 闲话WPF之五XAML中的类型转换 闲话WPF之十六WPF中的资源 2 闲话WPF之十九WPF中的传递事件 1 闲话WPF之二十WPF中的传递事件 2 闲话WPF之 ...
- 让PHPCms内容页支持JavaScript_
在PHPCms内容页中,出于完全考虑,默认是禁止JavaScript脚本的,所以我们在添加文章时,虽然加入了js代码,但实际上并没有起作用,而是以文本形式显示.如果要让内容页支持JavaScript, ...
- POJ 3468 A Simple Problem with Integers (区间更新+区间查询)
题目链接 Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operation ...
- 49、多线程创建的三种方式之继承Thread类
继承Thread类创建线程 在java里面,开发者可以创建线程,这样在程序执行过程中,如果CPU空闲了,就会执行线程中的内容. 使用Thread创建线程的步骤: 1.自定义一个类,继承java.lan ...
- Django 基础命令