UVA - 120 Stacks of Flapjacks(煎饼)
题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数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(煎饼)的更多相关文章
- Uva 120 - Stacks of Flapjacks(构造法)
UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- uva 120 stacks of flapjacks ——yhx
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- (白书训练计划)UVa 120 Stacks of Flapjacks(构造法)
题目地址:UVa 120 水题. 从最大的開始移,每次都把大的先翻到最上面,再翻到以下. 代码例如以下: #include <iostream> #include <cstdio&g ...
- UVa 120 Stacks of Flapjacks【构造法】
题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...
- UVA 120 Stacks of Flapjacks
每次从最底部开始处理,如果不是最大值,则把最大值翻到底部.这就是最优解.原理自己模拟一下就好... 注意半径不是从1开始.数据处理要仔细. #include <iostream> #inc ...
- UVaOJ 120 - Stacks of Flapjacks
120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...
- uva Stacks of Flapjacks
Stacks of Flapjacks 题目链接:Click Here~ 题目描写叙述: ...
- 【思维】Stacks of Flapjacks
[UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...
- Stacks of Flapjacks(栈)
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
随机推荐
- JetBrains IntelliJ IDEA(IJ)v2019.3.3/3.1/3.2/3.4/3.5 for mac/windows/linux 详细安装破解教程
手欠升级了IntelliJ IDEA到2019.3.3,原来的破解不可用,IntelliJ IDEA 2019.3.3破解办法如下,为方便自己使用记录下.======================= ...
- django中添加日志功能
官方文档 猛戳这里 在settings中配置以下代码 #LOGGING_DIR 日志文件存放目录 LOGGING_DIR = "logs" # 日志存放路径 if not os.p ...
- WinCC V7.5安装过程截图
- 使用windows函数SetWindowsHookEx实现键盘钩子
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- python编写banner获取的常用模块
模块的概念:模块也叫库,每个模块中都内置了大量的功能和函数.类和变量.它就像是积木,可以根据需要进行调用组合.模块就是程序,每个模块就是一个后缀为.py的Python程序.Python的模块分为标准模 ...
- chart 目录结构【转】
chart 是 Helm 的应用打包格式.chart 由一系列文件组成,这些文件描述了 Kubernetes 部署应用时所需要的资源,比如 Service.Deployment.PersistentV ...
- Codestorm:Counting Triangles 查各种三角形的个数
题目链接:https://www.hackerrank.com/contests/codestorm/challenges/ilia 这周六玩了一天的Codestorm,这个题目是真的很好玩,无奈只做 ...
- 新闻网大数据实时分析可视化系统项目——18、Spark SQL快速离线数据分析
1.Spark SQL概述 1)Spark SQL是Spark核心功能的一部分,是在2014年4月份Spark1.0版本时发布的. 2)Spark SQL可以直接运行SQL或者HiveQL语句 3)B ...
- CVE-2019-0708 漏洞分析及相关测试
在CVE-2019-0708公布后几天就已经尝试过复现该漏洞,但借助当时exp并没能成功复现反弹shell的过程遂放弃,故借助这次漏洞复现报告再来尝试复现该漏洞,因为还在大三学习中,有很多知识还没有掌 ...
- python isinstance()判断数据类型
举例: d = (1,2,3) print(isinstance(d,int)) #False print(isinstance(d,tuple)) #True print(isinstance(d, ...