Codeforces 451B Sort the Array(水题)

题目连接:Codeforces 451B Sort the Array

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

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

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll a[N], b[N];

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

int main() {
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> a[i], b[i] = a[i];
	sort(b, b + n);
	int l = 0, r = n - 1;
	while (l < n && b[l] == a[l])l++;
	while (r >= 0 && b[r] == a[r])--r;

	if (judge(l, r)) {
		if (r < l)
			l = r = 0;
		cout << "yes" << endl << l + 1<< " " << r + 1 << endl;
	}
	else
		cout << "no" << endl;
}

The desire of his soul is the prophecy of his fate
你灵魂的欲望,是你命运的先知。

原文地址:https://www.cnblogs.com/RioTian/p/13552124.html