GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Sunday 6 November 2022

[Solution] Indirect Sort Codeforces Solution



A. Indirect Sort
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a permutation a1,a2,,an of size n, where each integer from 1 to n appears exactly once.

You can do the following operation any number of times (possibly, zero):

  • Choose any three indices i,j,k (1i<j<kn).
  • If ai>ak, replace ai with ai+aj. Otherwise, swap aj and ak.

Determine whether you can make the array a sorted in non-descending order.

Input

Each test consists of multiple test cases. The first line contains a single integer t (1t5000) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (3n10) — the length of the array a.

The second line contains n integers a1,a2,,an (1ainaiaj if ij) — the elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 5104.

Output

For each test case, output "Yes" (without quotes) if the array can be sorted in non-descending order, and "No" (without quotes) otherwise.

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


Note

In the first test case, [1,2,3] is already sorted in non-descending order.

In the second test case, we can choose i=1,j=2,k=3. Since a1a3, swap a2 and a3, the array then becomes [1,2,3], which is sorted in non-descending order.

In the seventh test case, we can do the following operations successively:

  • Choose i=5,j=6,k=7. Since a5a7, swap a6 and a7, the array then becomes [1,2,6,7,4,5,3].
  • Choose i=5,j=6,k=7. Since a5>a7, replace a5 with a5+a6=9, the array then becomes [1,2,6,7,9,5,3].
  • Choose i=2,j=5,k=7. Since a2a7, swap a5 and a7, the array then becomes [1,2,6,7,3,5,9].
  • Choose i=2,j=4,k=6. Since a2a6, swap a4 and a6, the array then becomes [1,2,6,5,3,7,9].
  • Choose i=1,j=3,k=5. Since a1a5, swap a3 and a5, the array then becomes [1,2,3,5,6,7,9], which is sorted in non-descending order.

In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order

No comments:

Post a Comment