Codeforces Round 947 (Div. 1 + Div. 2 ABCDE) 视频讲解

A. Bazoka and Mocha’s Array

Problem Statement

Mocha likes arrays, so before her departure, Bazoka gave her an array a a a consisting of n n n positive integers as a gift.

Now Mocha wants to know whether array a a a could become sorted in non-decreasing order after performing the following operation some (possibly, zero) times:

  • Split the array into two parts — a prefix and a suffix, then swap these two parts. In other words, let a = x + y a=x+y a=x+y. Then, we can set a : = y + x a:= y+x a:=y+x. Here + + + denotes the array concatenation operation.

For example, if a = [ 3 , 1 , 4 , 1 , 5 ] a=[3,1,4,1,5] a=[3,1,4,1,5], we can choose x = [ 3 , 1 ] x=[3,1] x=[3,1] and y = [ 4 , 1 , 5 ] y=[4,1,5] y=[4,1,5], satisfying a = x + y a=x+y a=x+y. Then, we can set a : = y + x = [ 4 , 1 , 5 , 3 , 1 ] a:= y + x = [4,1,5,3,1] a:=y+x=[4,1,5,3,1]. We can also choose x = [ 3 , 1 , 4 , 1 , 5 ] x=[3,1,4,1,5] x=[3,1,4,1,5] and y = [   ] y=[\,] y=[], satisfying a = x + y a=x+y a=x+y. Then, we can set a : = y + x = [ 3 , 1 , 4 , 1 , 5 ] a := y+x = [3,1,4,1,5] a:=y+x=[3,1,4,1,5]. Note that we are not allowed to choose x = [ 3 , 1 , 1 ] x=[3,1,1] x=[3,1,1] and y = [ 4 , 5 ] y=[4,5] y=[4,5], neither are we allowed to choose x = [ 1 , 3 ] x=[1,3] x=[1,3] and y = [ 5 , 1 , 4 ] y=[5,1,4] y=[5,1,4], as both these choices do not satisfy a = x + y a=x+y a=x+y.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1000 1\leq t\leq 1000 1t1000). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 50 2\leq n\leq 50 2n50) — the length of the array a a a.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 6 1\leq a_i \leq 10^6 1ai106) — the elements of array a a a.

Output

For each test case, output “Yes” if a a a could become non-decreasing after performing the operation any number of times, and output “No” if not.

You can output “Yes” and “No” in any case (for example, strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive response).

Example

Example

input
3
6
1 1 4 5 1 4
5
7 9 2 2 3
3
1 2 3
output
No
Yes
Yes

Note

In the first test case, it can be proven that a a a cannot become non-decreasing after performing the operation any number of times.

In the second test case, we can perform the following operations to make a a a sorted in non-decreasing order:

  • Split the array into two parts: x = [ 7 ] x=[7] x=[7] and y = [ 9 , 2 , 2 , 3 ] y=[9,2,2,3] y=[9,2,2,3], then swap these two parts. The array will become y + x = [ 9 , 2 , 2 , 3 , 7 ] y+x = [9,2,2,3,7] y+x=[9,2,2,3,7].
  • Split the array into two parts: x = [ 9 ] x=[9] x=[9] and y = [ 2 , 2 , 3 , 7 ] y=[2,2,3,7] y=[2,2,3,7], then swap these two parts. The array will become y + x = [ 2 , 2 , 3 , 7 , 9 ] y+x=[2,2,3,7,9] y+x=[2,2,3,7,9], which is non-decreasing.

Solution

具体见文后视频。


Code

#include 
#define fi first
#define se second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 55;

int n;
int a[N];

void solve() {
   
	cin >> n;
	bool res = 1;
	for (int i = 1; i <= n; i ++)
		cin >> a[i], res &= (a[i] >= a[i - 1]);

	if (res) {
   
		cout << "Yes" << endl;
		return;
	} else if (a[1] < a[n]) {
   
		cout << "No" << endl;
		return;
	}
	for (int i = 1; i < n; i ++) {
   
		if (a[i] < a[i - 1]) break;
		bool flg = 1;
		for (int j = n - 1; j > i; j --)
			flg &= (a[j + 1] >= a[j]);
		if (flg) {
   
			cout << "Yes" << endl;
			return;
		}
	}

	cout << "No" << endl;
}

signed main() {
   
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio

你可能感兴趣的:(Codeforces,c++,算法)