I. original question
Description
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an arrayAConsistingNDistinct integers.
Unfortunately, the sizeAIs 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 ArrayA(In increasing order) by reversing exactly one segmentA? See definitions of segment and reversing in the notes.
Input
The first line of the input contains an integerN(1 digit ≤ DigitNLimit ≤ limit 105)-the size of ArrayA.
The second line containsNDistinct space-separated integers:A[1], bytesA[2], middle..., middleA[N] (1 limit ≤ limitA[I] Limit ≤ limit 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.
Sample Input
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
Hint
Sample 1. You can reverse the entire array to get [1, limit 2, limit 3], which is sorted.
Sample 3. No segment can be reversed such that the array will be sorted.
Definitions
A segment [L, Bytes,R] Of ArrayAIs the sequenceA[L], RoleA[LKeys + keys 1], keys..., keysA[R].
If you have an arrayAOf sizeNAnd you reverse its segment [L, Bytes,R], The array will become:
A[1], bytesA[2], middle..., middleA[LAccept-limit 2], denyA[LExecutor-cores 1], BuffersA[R], RoleA[RAudio-extract 1], audio..., audioA[LLimit + limit 1], limitA[L], RoleA[RLimit + limit 1], limitA[RPipeline + pipeline 2], pipeline..., PipelineA[NExecutor-cores 1], BuffersA[N].
Ii. Source Project (1)
#include<bits/stdc++.h>#define maxn 100005typedef long long LL;using namespace std;int a[maxn];int main(){ int n,x=1,y=1; cin>>n; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<n;i++){ if(a[i]>a[i+1]){ x=i;break; } } for(int i=n;i>=1;i--){ if(a[i]<a[i-1]){ y=i; break; } } for(int i=0;i<y-x;i++) { int t; t=a[x+i]; a[x+i]=a[y-i]; a[y-i]=t; } int flag=0; for(int i=1;i<n;i++){ if(a[i]>a[i+1]){ flag=1; break; } } if(flag) cout<<"no"<<endl; else{ cout<<"yes"<<endl<<x<<" "<<y<<endl; } return 0;}
Source Project (2)
#include<bits/stdc++.h>#define maxn 100005typedef long long LL;using namespace std;int a[maxn];int main(){ int n,x=1,y=1; cin>>n; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<n;i++){ if(a[i]>a[i+1]){ x=i;break; } } for(int i=n;i>=1;i--){ if(a[i]<a[i-1]){ y=i; break; } } reverse(a+x,a+y+1); int flag=0; for(int i=1;i<n;i++){ if(a[i]>a[i+1]){ flag=1; break; } } if(flag) cout<<"no"<<endl; else{ cout<<"yes"<<endl<<x<<" "<<y<<endl; } return 0;}
Iii. Solutions
Question Analysis:
If a sequence is given, can the whole sequence be monotonically incremented by a subsequence of The subsequence in reverse order? If yes, the subscript in reverse order is output; otherwise, no is output.
Ideas:
1. Traverse from left to right. The first subscript is smaller than the previous one;
2. Traverse from right to left. The first subscript is smaller than the last one;
3. Reverse the interval and determine whether the whole sequence increases monotonically.
Iv. Experiences
(1) The use of the reverse function is included in the header file <algorithm>.
(2) This is a simple sorting and simulation question.