1951B.md (3081B)
1 Source: https://codeforces.com/problemset/problem/1951/B 2 3 B. Battle Cows 4 5 time limit per test 6 7 1 second 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 [The HU - Shireg Shireg](https://youtu.be/SNCx4n2m5_o) 22 23 ඞ 24 25 There are $n$ cows participating in a coding tournament. Cow $i$ has a Cowdeforces rating of $a_i$ (all distinct), and is initially in position $i$. The tournament consists of $n-1$ matches as follows: 26 27 * The first match is between the cow in position $1$ and the cow in position $2$. 28 * Subsequently, each match $i$ is between the cow in position $i+1$ and the winner of match $i-1$. 29 * In each match, the cow with the higher Cowdeforces rating wins and proceeds to the next match. 30 31 You are the owner of cow $k$. For you, winning the tournament is not important; rather, you want your cow to win in as many matches as possible. As an acquaintance of the tournament organizers, you can ask them to swap the position of your cow with another cow only once, or you can choose to do nothing. 32 33 Find the maximum number of wins your cow can achieve. 34 35 Input 36 37 Each test contains multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows. 38 39 The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5, 1 \le k \le n$) — the number of cows and your cow's index. 40 41 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the Cowdeforces rating of the cows. It is guaranteed that $a_i$'s are pairwise different. 42 43 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. 44 45 Output 46 47 For each test case, print one integer: the maximum number of wins cow $k$ can achieve if you choose to swap (or do nothing) optimally. 48 49 Example 50 51 Input 52 53 ``` 54 3 55 56 6 1 57 58 12 10 14 11 8 3 59 60 6 5 61 62 7 2 727 10 12 13 63 64 2 2 65 66 1000000000 1 67 ``` 68 69 Output 70 71 ``` 72 1 73 2 74 0 75 ``` 76 77 Note 78 79 In the first test case, it is optimal to do nothing. Let $a'$ be the Cowdeforces rating of the cows in the original order (with your cow's rating bolded), then 80 81 * Initially, $a' = [\mathbf{12}, 10, 14, 11, 8, 3]$. 82 * Your cow plays against the cow with Cowdeforces rating $10$ and wins. $a' = [\mathbf{12}, 14, 11, 8, 3]$. 83 * Your cow plays against the cow with Cowdeforces rating $14$ and loses. 84 85 In total, your cow wins $1$ match. 86 87 In the second test case, it is optimal to swap your cow to position $3$. Then, let $a'$ be the Cowdeforces rating of the cows in the order after the swap. 88 89 * Initially, $a' = [7, 2, \mathbf{12}, 10, 727, 13]$. 90 * The cow with Cowdeforces rating $7$ plays against the cow with Cowdeforces rating $2$ and wins. $a' = [7, \mathbf{12}, 10, 727, 13]$. 91 * The cow with Cowdeforces rating $7$ plays against your cow, and your cow wins. $a' = [\mathbf{12}, 10, 727, 13]$. 92 * Your cow plays against the cow with Cowdeforces rating $10$ and wins. $a' = [\mathbf{12}, 727, 13]$. 93 * Your cow plays against the cow with Cowdeforces rating $727$ and loses. 94 95 In total, your cow wins $2$ matches.