B. Sort the Array
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
input
3
3 2 1
output
yes
1 3
input
4
2 1 3 4
output
yes
1 2
input
4
3 1 2 4
output
no
input
2
1 2
output
yes
1 1
Note

Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.

Sample 3. No segment can be reversed such that the array will be sorted.

Definitions

A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].

If you have an array a of size n and you reverse
its segment [l, r], the array will become:

a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].

—————————————————————切割线———————————————————————————

题意分析:

给定一个序列,是否能通过逆序它的一个子序列使得整个序列单调递增,假设能。输出须要逆序的下标,否则输出no

思路

1、从左到右遍历,第一次出现比前一个数小的下标;

2、然后从右往左遍历,第一次出现比后一个数小的下标。

3、将这段区间逆序,然后推断整个序列是否单调递增

#include<bits/stdc++.h>
#define maxn 100005
typedef long long LL;
using namespace std;
int a[maxn];
int main()
{
int n,x=1,y=1;
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<n;i++){
if(a[i]>a[i+1]){
x=i;break;
}
}
for(int i=n;i>=1;i--){
if(a[i]<a[i-1]){
y=i;
break;
}
}
reverse(a+x,a+y+1);
int flag=0;
for(int i=1;i<n;i++){
if(a[i]>a[i+1]){
flag=1;
break;
}
}
if(flag) cout<<"no"<<endl;
else{
cout<<"yes"<<endl<<x<<" "<<y<<endl;
}
return 0;
}

Codeforces Round #258 (Div. 2)——B. Sort the Array的更多相关文章

  1. Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

    题目链接:http://codeforces.com/contest/451/problem/B --------------------------------------------------- ...

  2. Codeforces Round #258 (Div. 2) B. Sort the Array

    题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...

  3. Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array

    解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅 ...

  4. Codeforces Round #258 (Div. 2)[ABCD]

    Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...

  5. Codeforces Round #258 (Div. 2) 小结

    A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...

  6. Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...

  7. Codeforces Round #258 (Div. 2)

    A - Game With Sticks 题目的意思: n个水平条,m个竖直条,组成网格,每次删除交点所在的行和列,两个人轮流删除,直到最后没有交点为止,最后不能再删除的人将输掉 解题思路: 每次删除 ...

  8. 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 ...

  9. Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

    http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题.. . 每次操作,都会拿走一个横行,一个竖行 ...

随机推荐

  1. OCP将结束容器产业这个颠覆性产业的标准格式之争

    编者注:本文英文版来自VentureBeat,中文版由天地会珠海分舵编译.当以Docker为首的容器正在席卷全球.蔚然成风的颠覆着原来的应用开发和公布方式的时候,容器标准之争却从来没有消停过.而标准之 ...

  2. 强悍的 vim —— 删除空行、删除注释以及加注释解注释

    强悍的 vim -- 删除空行.删除注释以及加注释解注释 原文 https://blog.csdn.net/lanchunhui/article/details/51588198 1. 删除空行空行的 ...

  3. TCP连接状态详解

    tcp状态: LISTEN:侦听来自远方的TCP端口的连接请求 SYN-SENT:再发送连接请求后等待匹配的连接请求 SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认 ES ...

  4. 兼容MIUI5和MIUI6的开启悬浮窗设置界面

    前一段时间项目中需要对MIUI的悬浮窗开启设置界面进行了引导和跳转,MIUI6中又改变了开启悬浮窗设置的位置,在苦苦寻觅之后,找到了解决的方法,贴出来以方便大家参考和使用. @Override pub ...

  5. 解决root登录 -bash-4.2# 的问题

  6. 003 python 注释/数据类型/运算符/输入输出/格式化输出

    集成开发环境 pycharm 工欲善其事,必先利其器 pycharm是具备一般的python ide的功能,同时呢支持调试,语法高亮,代码管理,智能提示 加快快发的速度,提高开发效率 注释 what ...

  7. HDU 2191 悼念512汶川大地震

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  8. iOS 创建静态库文件时去掉当中的Symbols

    在project中创建静态库文件时.默认会将一些Symbols加到静态库文件里.这样做有两个缺点: 1.假设引用静态库文件的project中发生了bug.就会直接跳转到静态库的源代码. 也许有人问:静 ...

  9. [Angular] Create a custom validator for template driven forms in Angular

    User input validation is a core part of creating proper HTML forms. Form validators not only help yo ...

  10. BZOJ3626: [LNOI2014]LCA(树链剖分+线段树)

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q ...