UVa 120 (构造) Stacks of Flapjacks
这题求解的过程和选择排序非常相似。
反转的过程中分为无序(在前面)和有序(在后面)两个部分,一开始视为全部为无序。
在无序部分中找到最大的元素,先把它翻到最前面,然后再反转到无序部分的最后面。这样该元素就成为有序的部分。
而且在算法执行的过程中不会影响到已经构造好的有序部分。
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstdio>
using namespace std; const int maxn = + ;
int n, a[maxn]; 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;
if(p == i) continue;
if(p > ) flip(p);
flip(i);
}
puts("");
} return ;
}
代码君
UVa 120 (构造) Stacks of Flapjacks的更多相关文章
- 【uva 120】Stacks of Flapjacks(算法效率--构造法+选择排序思想)
题意:有N张正在锅里的一叠煎饼,每张都有一个数字,代表其大小.厨师每次可以选择一个数k,把从锅底开始数第k张上面的煎饼全部翻过来,即原来在上面的煎饼现在到了下面.要求设计一种方法使得所有煎饼按照从小到 ...
- 【例题 8-1 UVA 120 】Stacks of Flapjacks
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从大到小安排. 显然想让第i大的数字归位 只要让他翻到最上面,然后再翻回来就ok了 即operate(pos[i]) -> o ...
- 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(构造法)
题目地址:UVa 120 水题. 从最大的開始移,每次都把大的先翻到最上面,再翻到以下. 代码例如以下: #include <iostream> #include <cstdio&g ...
- 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 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 ...
随机推荐
- discuz!X2.5技术文档
discuz!系统常量: DISCUZ_ROOT //网站根目录 TIMESTAMP //程序执行的时间戳 CHARSET //程序的编码类型 FORMHASH //HASH值 其余 ...
- delphi xe memory leak produced in WSDLLookup.pas
constructor TWSDLLookup.Create; begin FLookup := TDictionary<string, Variant>.Create; end; des ...
- Spark Streaming揭秘 Day25 StreamingContext和JobScheduler启动源码详解
Spark Streaming揭秘 Day25 StreamingContext和JobScheduler启动源码详解 今天主要理一下StreamingContext的启动过程,其中最为重要的就是Jo ...
- PythonCrawl自学日志(3)
2016年9月21日09:21:431.爬虫的抓取周期:(1)首先生成初始请求爬第一个url,并指定一个回调函数被称为与下载这些请求的响应.(2)第一个请求执行通过调用 start_requests( ...
- Mysql 如何删除数据表中的重复数据!
1.使用distinct查询所有不重复的记录 2.创建数据表相同结构的临时表,将第一步的数据复制进去 create temporary table if not exists student_temp ...
- IOS 学习参考
IOS 开发 http://code4app.com/ios/%E5%AE%9E%E6%97%B6%E6%9B%B4%E6%96%B0%E7%9A%84%E6%9B%B2%E7%BA%BF%E5%9B ...
- iOS runtime 与 runloop
runtime是运行时机制,就是运行到的时候才会执行的机制(类似于栏加载)属于时间先后性质的 runloop 是主线程的运行的意思 属于 (loop) 循环性质的 以下是 具体点的 解释: run ...
- springmvc 数据对象回绑
springmvc中,由页面 post到 controller,对象可以在form里面设置modelAttribute达到回绑的目的. 但是如果对象里面有复杂的非String,int的对象,则要在co ...
- hadoop Safe mode is ON 的解决办法
hadoop Safe mode is ON 的解决办法 搭了一个hadoop集群环境,近期总是出现读写文件错误的情况,查看name node的日志显示 (Safe mode is ON) Safe ...
- [转载]js javascript 判断字符串是否包含某字符串,String对象中查找子字符,indexOf
var Cts = "bblText"; if(Cts.indexOf("Text") > 0 ) { alert('Cts中包含Text字符串'); }