UVa 120 煎饼
https://vjudge.net/problem/UVA-120
题意:颠倒连续子序列,使之成为升序。
思路:按照从大到小的顺序,依次选择出一个数字进行分析:
①如果该数字已经在正确的位置上,则不用管。
②如果该数字已经在最顶端,则从它该在的位置一直到顶端颠倒。
③如果该数字不在最顶端,则应先把它搞到最顶端,再从它该在的位置一直到顶端颠倒。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
using namespace std; const int maxn = ;
int ans[maxn];
int coo[maxn];
int coo2[maxn];
int path[];
int k;
int num; string line; bool cmp(int a, int b)
{
return a > b;
} void convert(int t) //颠倒连续子序列
{
memcpy(coo2, ans, sizeof(ans));
for (int i = ; i <= t; i++)
{
ans[i] = coo2[t - i + ];
}
} void solve()
{
memcpy(coo, ans, sizeof(ans));
num = ;
sort(coo + , coo + k + , cmp);
for (int i = ; i <= k; i++)
{
int s = coo[i];
if (ans[k-i+] == s) continue; //如果已经在正确的位置上
for (int j = ; j <= k; j++)
{
if (ans[j] == s)
{
if (j != ) //如果未在顶端,先将它颠倒至顶端
{
convert(j);
path[num++] = k - j + ;
}
break;
}
}
convert(k - i + ); //颠倒至正确位置
path[num++] = i ;
}
path[num++] = ;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (getline(cin,line ))
{
k = ;
int x;
stringstream ss(line);
while (ss >> x) ans[++k] = x;
for (int i = ; i < k; i++)
cout << ans[i] << " ";
cout << ans[k] << endl;
solve();
for (int i = ; i < num - ; i++)
cout << path[i] << " ";
cout << path[num - ] << endl;
}
return ;
}
UVa 120 煎饼的更多相关文章
- 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(煎饼)
题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数k,把从锅底开始数第k张以及其上面的煎饼全部翻过来,最终使煎饼有序排列(锅顶最小,锅底最大). 分析:依次从锅底向上,优先排数字最大的煎饼.每次 ...
- UVa 120 Stacks of Flapjacks【构造法】
题意:给出n张煎饼,从上到下输入,每张煎饼上面都有一个数字,厨师每次可以选择第k张煎饼,进行翻转操作,设计一种方法使得所有煎饼按照从小到大排序(最上面的煎饼最小) 首先是这个翻转的操作,如下图 如图所 ...
- 【uva 120】Stacks of Flapjacks(算法效率--构造法+选择排序思想)
题意:有N张正在锅里的一叠煎饼,每张都有一个数字,代表其大小.厨师每次可以选择一个数k,把从锅底开始数第k张上面的煎饼全部翻过来,即原来在上面的煎饼现在到了下面.要求设计一种方法使得所有煎饼按照从小到 ...
- 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 Stacks of Flapjacks
每次从最底部开始处理,如果不是最大值,则把最大值翻到底部.这就是最优解.原理自己模拟一下就好... 注意半径不是从1开始.数据处理要仔细. #include <iostream> #inc ...
- 紫书 例题8-1 UVa 120(构造法)
#include<cstdio> #include<iostream> #include<sstream> #include<algorithm> #d ...
随机推荐
- 008-spring cloud gateway-路由谓词RoutePredicate、RoutePredicateFactory
一.概述 Spring Cloud Gateway将路由作为Spring WebFlux HandlerMapping基础结构的一部分进行匹配. Spring Cloud Gateway包含许多内置的 ...
- 008-jdk1.7版本新特性
一.JDK1.7 名称:Dolphin(海豚) 发布日期:2011-07-28 新特性: 1.1.switch-case中可以使用字串 区分大小写.Java编译器通过switch使用String对象的 ...
- abap 基本知识
sap gui 安装教程:http://www.itpub.net/forum.php?mod=viewthread&tid=2090890 1:abap 基本数据类型: i(整型),f(浮点 ...
- ConcurrentModificationException
//需求:如何集合中有给定的元素就在集合中在插入一个元素public class ListIteratorDemo2 { public static void main(String[] args) ...
- tf.nn.embedding_lookup函数的用法
关于np.random.RandomState.np.random.rand.np.random.random.np.random_sample参考https://blog.csdn.net/lanc ...
- C#集合中的Add与AddRange方法
C#.NET的集合主要位于System.Collections和System.Collections.Generic(泛型)这两个namespace中. 1.System.Collections 比如 ...
- [LeetCode] 183. Customers Who Never Order_Easy tag: SQL
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...
- Keepalived+HAproxy实现高可用负载均衡
总概: Keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器的状态, ...
- 表单验证——JqueryValidator、BootstrapValidator
表单验证两种方式: 1.JqueryValidator <!DOCTYPE html> <html lang="en"> <head> < ...
- Lower Power with CPF(四)
CPF从Front-end到Back-end(RTL--GDSII)的整个流程: 1)Creating a CPF file:来在前端就建立lower power的规范. 2)检查CPF文件的正确性, ...