Codeforces Round #258 (Div. 2) . Sort the Array 贪心
B. Sort the Array
题目连接:
http://codeforces.com/contest/451/problem/B
Description
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.
Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a.
The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).
Output
Print "yes" or "no" (without quotes), depending on the answer.
If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.
Sample Input
3
3 2 1
Sample Output
yes
1 3
Hint
题意
给你n个数,然后你可以交换一个子串的顺序,问你能不能使得这n个数是单调递增的。
题解:
贪心去交换就好了,交换完再check一下就行。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n;
int a[maxn];
bool check()
{
for(int i=2;i<=n;i++)
if(a[i]<a[i-1])return false;
return true;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
int j=i+1;
for(;j<=n;j++)
if(a[j]>=a[i])
break;
if(j!=i+1)
{
reverse(a+i,a+j);
if(check())
{
printf("yes\n");
printf("%d %d",i,j-1);
}
else
printf("no\n");
return 0;
}
}
if(check())
{
printf("yes\n");
printf("%d %d",1,1);
}
else
printf("no\n");
}
Codeforces Round #258 (Div. 2) . Sort the Array 贪心的更多相关文章
- Codeforces Round #555 (Div. 3) E. Minimum Array (贪心,二分,set)
题意:给你两个长度为\(n\)的数组\(a\)和\(b\),元素值在\([0,n-1]\),可以对\(b\)数组的元素任意排序,求新数组\(c\),满足\(c_i=(a_i+b_i)\ mod\ n\ ...
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #258 (Div. 2) 小结
A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...
- Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)
题目链接:http://codeforces.com/contest/451/problem/B --------------------------------------------------- ...
- Codeforces Round #258 (Div. 2)——B. Sort the Array
B. Sort the Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array
解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅 ...
- Codeforces Round #258 (Div. 2)
A - Game With Sticks 题目的意思: n个水平条,m个竖直条,组成网格,每次删除交点所在的行和列,两个人轮流删除,直到最后没有交点为止,最后不能再删除的人将输掉 解题思路: 每次删除 ...
- Codeforces Round #258 (Div. 2)(A,B,C,D)
题目链接 A. Game With Sticks time limit per test:1 secondmemory limit per test:256 megabytesinput:standa ...
- Codeforces Round #258 (Div. 2)-(A,B,C,D,E)
http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题.. . 每次操作,都会拿走一个横行,一个竖行 ...
随机推荐
- 翻译:探索GLSL-用几何着色器(着色器库)实现法线可视化
翻译:探索GLSL-用几何着色器(着色器库)实现法线可视化 翻译自: Exploring GLSL – Normal Visualizer with Geometry Shaders (Shader ...
- Sublime Text 之运行 js 方法[2015-5-6更新mac下执行js]
昨天说完<Sublime Text 2 绿化与汉化 [Windows篇]>,今天我们来说说怎么用st直接运行 js 吧.群里的小伙伴一直对我的 ST 能直接运行js感到非常好奇,今天我就公 ...
- [转载]为什么有些MP4文件在Chrome浏览器上播放不了?
http://blog.sina.com.cn/s/blog_6bb7ebcc0101c2ja.html Chrome浏览器支持HTML5,它支持原生播放部分的MP4格式(不用通过Flash等插件). ...
- css左右等高问题
先看看预览效果:http://lgdy.whut.edu.cn/index.php?c=home&a=detail&id=3394 再来谈谈css左右等高的应用场景:在内容管理系统(c ...
- ZYNQ. DMA基本用法
DMA环路测试 vivadoblock zynq7 + dma +fifo sdk 中可以导入 demo demo 中 默认都是 一个字节8bit数据 的测试程序. 如果是其他长度的数据,不仅要修改数 ...
- Java 连接远程Linux 服务器执行 shell 脚本查看 CPU、内存、硬盘信息
pom.xml jar 包支持 <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch& ...
- shell加密
如何保护自己编写的shell程序要保护自己编写的shell脚本程序,方法有很多,最简单的方法有两种:1.加密 2.设定过期时间,下面以shc工具为例说明: 一.下载安装shc工具shc是一个加密she ...
- mysql添加事件
begin declare debug int; set @debug = 0; if @debug = 1 then insert into task_monitor(info) values('s ...
- 方法调用---springMVC中调用controller的方法
我们有一个路由StudentController,里面有一个方法count().如果要在另外一个GradeController中调用count()方法有2种方式: 因为StudentControlle ...
- 解决导入Android例子时“Unable to resolve target 'android-x' ”的错误
今天导入一个Android的例子程序,出现了Unable to resolve target 'android-2' 的错误. 最后google之后才发现原来是 ADK版本 :---API Level ...