CODEFORCES Day17

题目:

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


 
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100000;
int a[maxn],b[maxn];
int main()
{
    int i,left,right,n;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        sort(b+1,b+n+1);
        for(left=1;left<=n;left++)
        {
            if(a[left]!=b[left]){break;}
        }
        if(left>n){cout<<"yes"<<endl;cout<<1<<" "<<1<<endl;}
        else
            {
                for(right=n;right>0;right--)
                {
                    if(a[right]!=b[right]){break;}
                }
            
                for(i=left;i<right;i++)
                {
                    if(a[i]<=a[i+1]){cout<<"no"<<endl;break;}
                }
                if(i==right){cout<<"yes"<<endl;cout<<left<<" "<<right<<endl;}
            }
    }
    return 0;
}
 

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.

思路:代码

上一篇:Day17 包装类 日期


下一篇:一起重新开始学大数据-java篇-DAY17-集合体系: