题目描述:
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.
题意:
大意就是让你去选择a数组的一个区间中的l与r进行倒置,使得改变后的数组从小到大排序。
解题思路:
想法就是找到第一个违背a[i+1]>=a[i]的i作为l,接着往后遍历找到最后一个违背a[i]>=a[b]的i,作为e。
然后按照将【l,r】区间的数据进行倒置,接着遍历检查是否有违背a[i+1]>=a[i]的,有就输出“no”,否则输出“yes”。
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
using namespace std;
#define MAX 1000006
int a[MAX],b[MAX];
int ispr[MAX],pri[MAX];
int main()
{
int n;
cin>>n;
for(int i=1; i<=n; i++)cin>>a[i];
int b=1,f=0,e=1;
for(int i=1; i<n; i++)
{
if(a[i+1]<a[i]&&!f)
{
b=i;
f=1;
e=i+1;
}
else if(a[b]>a[i]&&f)
{
e=i;
}
}
if(a[n]<a[b])e=n;
reverse(a+b,a+e+1);
for(int i=1; i<n; i++)
{
if(a[i+1]<a[i])
{
cout<<"no";
return 0;
}
}
cout<<"yes\n"<<b<<" "<<e;
}