题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数k,把从锅底开始数第k张以及其上面的煎饼全部翻过来,最终使煎饼有序排列(锅顶最小,锅底最大)。

分析:依次从锅底向上,优先排数字最大的煎饼。每次找未排好序列中数字最大的煎饼,并把他翻到锅顶,再将整个未排好序的序列翻转,这样该数字就到了当所有煎饼有序排列时其所在的位置。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
string s;
vector<int> a;
vector<int> b;
int len;
void Reverse(int st, int et){//翻转
int len = (et - st) / ;
for(int i = ; i <= len; ++i){
swap(a[i], a[et - i]);
}
}
bool judge(){
for(int i = ; i < len; ++i){
if(a[i] != b[i]) return false;
}
return true;
}
int main(){
while(getline(cin, s)){
a.clear();
b.clear();
stringstream ss(s);
int ma = ;//最大值
int x;
while(ss >> x){
a.push_back(x);
b.push_back(x);
ma = Max(ma, x);
}
sort(b.begin(), b.end());
len = a.size();
for(int i = ; i < len; ++i){
if(i) printf(" ");
printf("%d", a[i]);
}
printf("\n");
int t = len;
while(){
if(judge()) break;//已排好序
--t;
for(int i = ; i < len; ++i){
if(a[i] == b[t]){
if(i != t){//若当前要排的数已在其排好序的位置则不做翻转
if(i != ){//若不在顶部
Reverse(, i);
printf("%d ", len - i);
}
Reverse(, t);
printf("%d ", len - t);
}
break;
}
}
}
printf("0\n");
}
return ;
}

UVA - 120 Stacks of Flapjacks(煎饼)的更多相关文章

  1. Uva 120 - Stacks of Flapjacks(构造法)

    UVA - 120  Stacks of Flapjacks Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld &a ...

  2. uva 120 stacks of flapjacks ——yhx

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  3. (白书训练计划)UVa 120 Stacks of Flapjacks(构造法)

    题目地址:UVa 120 水题. 从最大的開始移,每次都把大的先翻到最上面,再翻到以下. 代码例如以下: #include <iostream> #include <cstdio&g ...

  4. UVa 120 Stacks of Flapjacks【构造法】

    题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...

  5. UVA 120 Stacks of Flapjacks

    每次从最底部开始处理,如果不是最大值,则把最大值翻到底部.这就是最优解.原理自己模拟一下就好... 注意半径不是从1开始.数据处理要仔细. #include <iostream> #inc ...

  6. UVaOJ 120 - Stacks of Flapjacks

    120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...

  7. uva Stacks of Flapjacks

                                                     Stacks of Flapjacks  题目链接:Click Here~ 题目描写叙述:     ...

  8. 【思维】Stacks of Flapjacks

    [UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...

  9. Stacks of Flapjacks(栈)

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

随机推荐

  1. 一、linux基础-对文件操作

    1.1文件夹创建-复制-移动-重命名-删除1.创建文件夹mkdir zjbdir 2.复制文件/文件夹复制文件到:当前目录cp -r zjbdir  zjbdir201600819复制文件到:当前目录 ...

  2. 二、js中基础知识

    该篇文章主要是强化一下自己javaScript的基础,让写代码变得更轻松些.基础好的请忽略.    JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解 ...

  3. 解题报告:CF622F

    懒得码字了: 题目链接:CF622F 很简单的数论题,紫题显然是过了些,(不要说... 对于这个式子,是一个\(k+1\)次的多项式,插\(k+2\)次值就好了,烦人的是处理逆元,我的费马小定理显然是 ...

  4. 45 孩子们的游戏(圆圈中最后剩下的数) + list操作总结+ for_each多记忆容易忘记

    题目描述 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指 ...

  5. SciPy 特殊函数

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  6. shell 脚本终止进程

    参考:https://blog.csdn.net/zhaoyue007101/article/details/7699259 $(pidof 进程名关键字)

  7. Linux-nftables

    Linux-nftables https://netfilter.org/ https://netfilter.org/projects/iptables/index.html https://net ...

  8. GoJS API学习

    var node = {}; node["key"] = "节点Key"; node["loc"] = "0 0";// ...

  9. Android拷贝工程不覆盖原工程的配置方法

    http://www.2cto.com/kf/201203/125131.html 在Eclipse中改包名的时候选择refactor-->rename,勾选Rename subpackages ...

  10. mac搭建nginx

    0.介绍 Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler ...