题目链接:http://codeforces.com/contest/451/problem/B
----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
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.
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).
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.
3 3 2 1
yes 1 3
4 2 1 3 4
yes 1 2
4 3 1 2 4
no
2 1 2
yes 1 1
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.
Sample 3. No segment can be reversed such that the array will be sorted.
Definitions
A segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].
If you have an array a of size n and you reverse its segment [l, r], the array will become:
a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].
题意:
寻找把数字串的某一段字串反转后是否能使数字串变为升序;并输出需反转的字串的起始和结束点;
思路:
把数字串排序后寻找和原数字串不同的子段是否是呈反转关系的。
代码例如以下:
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; int a[100017]; struct NUM { int x,id; }b[100017]; bool cmp(NUM p, NUM q) { return p.x < q.x; } int main() { int n, i, j; while(~scanf("%d",&n)) { for(i = 1; i <= n; i++) { scanf("%d",&a[i]); b[i].id = i; b[i].x = a[i]; } sort(b+1,b+n+1,cmp); for(i = 1; i <= n; i++)//寻找不同段的開始点 { if(b[i].id != i) break; } if(i == n+1) { printf("yes 1 1 "); continue; } for(j = n; j >= 1; j--)//寻找不同段的末尾点 { if(b[j].id != j) break; } int tt = i; int t = j; for(; i <= j; i++)//反向比較不同段 { if(b[i].x != a[t--]) break; } if(i == j+1) { printf("yes %d %d ",tt,j); } else printf("no "); } return 0; }