题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数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. springcloud gateway 项目打包部署运行

    新建一个springboot项目然后做了一个小demo跳转到baidu pom <?xml version="1.0" encoding="UTF-8"? ...

  2. Day2-J-逃离迷宫-HDU-1728

    给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位 ...

  3. 「NOIP2010」引水入城

    传送门 Luogu 解题思路 第一问很好做,只要总第一行的每一个点都跑一边dfs,判断最后一行是否有点标记不了即可. 考虑处理第二问. 其实这一问就是: 把第一行的点都看做是对最后一行一些点的覆盖,求 ...

  4. PHP几个快速读取大文件例子

    PHP几个快速读取大文件例子 感谢 把我给崩了 的投递 时间:2014-10-16 来源:三联 在PHP中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents ...

  5. Vue-cli3与springboot项目整合打包

    一.需求        使用前后端分离编写了个小程序,前端使用的是vue-cli3创建的项目,后端使用的是springboot创建的项目,部署的时候一起打包部署,本文对一些细节部分进行了说明.   二 ...

  6. Jquery元素筛选、html()和text()和val三者区别

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Oracle性能优化小结

    Oracle性能优化小结 原则一.注意where子句中的连接顺序 Oracle采用自下而上的顺序解析where子句,根据这个原理,表之间的连接必须卸载其他where条件之前,哪些可以滤掉最大数量记录的 ...

  8. 2017 青岛网络赛 Chenchen, Tangtang and ZengZeng

    Chenchen, Tangtang and ZengZeng are starting a game of tic-tac-toe, played on a 3 × 3 board. Initial ...

  9. LINQ---查询表达式的结构

    重要事项: 子句必须按照一定的顺序出现 from子句和select...group子句这两部分是必须的 其他子句是可选的 在LINQ查询表达式中,select子句在表达式最后. 可以后任意多的from ...

  10. python 字典复制(存疑)

    import copy x = {'a':1,'b':[2,3,4]} y = x.copy() z = copy.deepcopy(x) print(x) print(y) print(z) pri ...