[Codeforces451B]Sort the Array

描述 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.

输入格式 InputFormat

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

输出格式 OutputFormat

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.

样例输入 SampleInput

46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100

样例输出 SampleOutput

1 11

Codeforces 451B

代码 Code


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x7fffffff/11.27;
int a[100005],b[100005]; 
int i,j,t,n,m,l,r,k,z,y,x;
inline void read(int &x)
    x=0;char ch=getchar();
    while (ch<'0' || ch>'9') ch=getchar();
    while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
int main()
    for (i=1;i<=n;i++) read(a[i]),b[i]=a[i];
    for (i=1;i<=n;i++) if (a[i]!=b[i]) break;
    if (i>n)
        printf("yes\n1 1");
        return 0;
    for (i=n;i>=1;i--) if (a[i]!=b[i]) break;
    for (i=l;i<=r;i++) if (a[i]!=b[r-(i-l)]) break;
    if (i>r) printf("yes\n%d %d\n",l,r);
    else printf("no\n");
    return 0;