2231B.md (2257B)
1 Source: https://codeforces.com/problemset/problem/2231/B 2 3 B. Another Sorting Problem 4 5 time limit per test 6 7 1.5 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given an array $a_1, a_2, \ldots, a_n$. You can perform the following operation on this array at most once: 22 23 * Choose a positive integer $k$, as well as a subsequence $b_1, b_2, \ldots, b_m$ of the array $a$$^{\text{∗}}$ and add $k$ to each element of this subsequence, that is, for each $i$ perform $a_{b_i} := a_{b_i} + k$. 24 25 You need to determine whether it is possible to sort the array in nondecreasing order by performing this operation at most once. 26 27 $^{\text{∗}}$A sequence $b$ is a subsequence of a sequence $a$ if $b$ can be obtained from $a$ by the deletion of several (possibly, zero or all) element from arbitrary positions. 28 29 Input 30 31 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. 32 33 In the first line of each test case, one integer $n$ is given ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. 34 35 In the second line of each test case, $n$ integers $a_1, a_2, \ldots, a_n$ are given ($1 \leq a_i \leq 10^9$). 36 37 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 38 39 Output 40 41 For each test case, print "Yes" if it is possible to sort the array by performing the operation at most once. Otherwise, print "No". You may print the answer in any case. For example, "YeS", "YES", "NO", "nO" will also be accepted. 42 43 Example 44 45 Input 46 47 ``` 48 8 49 50 4 51 52 1 2 3 4 53 54 5 55 56 10 5 9 4 4 57 58 7 59 60 8 3 9 4 10 5 11 61 62 6 63 64 5 3 10 5 9 7 65 66 3 67 68 2 1 1 69 70 5 71 72 2 1 1 2 1 73 74 3 75 76 3 1 2 77 78 5 79 80 2 1 2 3 1 81 ``` 82 83 Output 84 85 ``` 86 YES 87 88 NO 89 90 YES 91 92 NO 93 94 YES 95 96 YES 97 98 YES 99 100 NO 101 ``` 102 103 Note 104 105 In the first test case, the array is already sorted, so the operation does not need to be performed at all. 106 107 In the second test case, it can be shown that it is impossible to sort the array using such an operation. 108 109 In the third test case, choose $k = 6$ and choose the subsequence $[2, 4, 6]$ as $b$. Then after this operation, the array $a$ becomes $[8, \textbf{9}, 9, \textbf{10}, 10, \textbf{11}, 11]$ (the elements of the chosen subsequence are shown in bold).