Sort the Array

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sort the Array相关的知识,希望对你有一定的参考价值。

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.

Example

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

 

5
69 37 27 4 2
yes
1 5

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100000;     /*找出左右第一个不同的两个数然后反转其中的看看是否可以循序*/
int main()
{
        int n,m=0,a[N],b[N],c[N],i=0,flag=1,t;
        int l=0,h=0;
        cin>>n;
        for(int j=0;j<n;j++) /*循环输入数据*/
            {
               scanf("%d",&a[j]);
                c[j]=b[j]=a[j];
           }
        for( i=0;i<n-1;i++)  /*找出左边大于右边的第一个数*/
        {
            if(a[i]>a[i+1])
            {
                l=i;
                break;
            }
        }
        for( i=n-1;i>=1;i--)/*找出右边大于左边的第一个数*/
        {
            if(a[i]<a[i-1])
            {
                h=i;
                break;
            }
        }
   /*反转数组*/
        for(int j=h;j>=l;j--)
        {
            b[l+m]=a[j];
            m++;
        }
   /*反转数组*/
        for( i=0;i<n-1;i++) /*比较*/
        {
            if(b[i]>b[i+1])
            {
                flag = 0;
                break;
            }
        }

        if(flag==0)
            cout<<"no"<<endl;
        else if(flag==1)
        {
            cout<<"yes"<<endl;
            printf("%d %d\n",l+1,h+1);
        }
}

  

以上是关于Sort the Array的主要内容,如果未能解决你的问题,请参考以下文章

Sort the Array

Codeforces 258B Sort the Array

B. Sort the Array1300 / 排序

python [代码片段]一些有趣的代码#sort

js代码片段: utils/lcoalStorage/cookie

环境初始化 Build and Install the Apache Thrift IDL Compiler Install the Platform Development Tools(代码片段