码迷,mamicode.com
首页 > 其他好文 > 详细

Sort the Array

时间:2017-07-17 23:47:12      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:nsis   can   左右   tun   possible   index   scan   sel   --   

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

标签:nsis   can   左右   tun   possible   index   scan   sel   --   

原文地址:http://www.cnblogs.com/ljzh/p/7197517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!