Codeforces 451B Sort the Array(水题)

题目连接:Codeforces 451B Sort the Array

题目大意:给出一个长度为n的序列,可以有一次机会旋转a[l]到a[r]之间的数,问说可否形成一个递增序列。

解题思路:将数组排下序,然后从前向后,从后向前寻找不同到位置,这段l~r是一定要旋转的,然后判断旋转后的符不符合递增。注意l>r的情况。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 1e5+5;

int n, arr[maxn], pos[maxn];

bool judge (int l, int r) {
    for (int i = 0; i + l <= r; i++) {
        if (arr[l+i] != pos[r-i])
            return false;
    }
    return true;
}

int main () {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
        pos[i] = arr[i];
    }

    sort(pos, pos + n);
    int l = 0, r = n-1;
    while (l < n && pos[l] == arr[l]) l++;
    while (r >= 0 && pos[r] == arr[r]) r--;

    if (judge(l, r)) {
        if (r < l)
            l = r = 0;
        printf("yes\n%d %d\n", l+1, r+1);
    } else
        printf("no\n");
    return 0;
}

你可能感兴趣的:(Codeforces 451B Sort the Array(水题))