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 ...
随机推荐
- Mixin类的实现
python类的多重继承由于C3算法的原因导致实现时需要提前规划先后顺序才能正常使用. 这会让人在python中使用多重继承时感到十分的麻烦. 而Mixin类则为我们带来了自由的多重继承和插拔式的舒适 ...
- 公用表表达式(CTE) with as 片段
1.为什么用CTE,而不用表变量, declare @t table(CountryRegionCode nvarchar(3)) insert into @t(CountryRegionCode) ...
- python符号//、%和/运算
a = 9 print('这是%运算的结果'+str(a%2)) print('这是//运算的结果'+str(a//2)) print('这是/运算的结果'+str(a/2))运算结果为 这是%运算的 ...
- 简单模拟IOC容器:为添加了@Autowired的属性赋值(初始值)
创建@Autowired注解 package com.zzj.test; import java.lang.annotation.ElementType; import java.lang.annot ...
- 二 SVN代码冲突的解决
问题: A和B都是最新的代码,A修改了代码提交了,B也修改了代码,但是B提交的时候出现冲突的问题. 解决方案:编辑冲突 解决冲突: 方法一:将文件里面冲突的描述去掉,重新提交 方法二:软件解决冲突
- jmeter里面Dug Sampler 和json提取器的用法
1.编写用户详情请求 2.查看结果树 一级一级往上查找父集 3.添加json提取器 步骤:点击[用户详情]请求->添加->后置处理器->json提取器 把查看结果树里面的JSON P ...
- JS+ES6 - 向数组的开头添加一个或更多元素
- jsp中include的两种用法
JSP中的include的两种用法 1.两种用法 <%@ include file=” ”%> <jsp:include page=” ” flush=”true”/> 2.用 ...
- 解题报告:luogu P3853 [TJOI2007]路标设置
题目链接:P3853 [TJOI2007]路标设置 是个水二分,那你还\(WA\).很简单,就是练了练和早上那题相似的题. 二分答案即可,复杂度\(O(Nlogl)\),可以通过本题. 不过,需要注意 ...
- javascript 原型链污染
原理①javascript中构造函数就相当于类,并且可以将其实例化 ②javascript的每一个函数都有一个prototype属性,用来指向该构造函数的原型同样的javascript的每一个实例对象 ...