http://codeforces.com/problemset/problem/451/B
取前后第一个不满足条件的位置,逆序,判断。
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; int main() { int n,a[100005],left = 1,right = 1; cin >> n; for(int i = 1;i <= n;i++) { cin >> a[i]; } for(int i = 1;i <= n-1;i++) { if(a[i] > a[i+1]) { left = i; break; } } for(int i = n;i > 0;i--) { if(a[i] < a[i-1]) { right = i; break; } } reverse(a+left,a+right+1); int flag = 1; for(int i = 1;i <= n-1;i++) { if(a[i] > a[i+1]) { flag = 0; break; } } if(flag) { printf("yes "); printf("%d %d ",left,right); } else { printf("no "); } return 0; }