CF BUDDY
← Problems·

1951B · Battle Cows

1200 · binary search, data structures, greedy

Problem: There are nn cows participating in a coding tournament. Cow ii has a Cowdeforces rating of aia_i (all distinct), and is initially in position ii. The tournament consists of n1n-1 matches as follows:

  • The first match is between the cow in position 11 and the cow in position 22.
  • Subsequently, each match ii is between the cow in position i+1i+1 and the winner of match i1i-1.
  • In each match, the cow with the higher Cowdeforces rating wins and proceeds to the next match.

You are the owner of cow kk. 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.

Find the maximum number of wins your cow can achieve.

Input Format: Each test contains multiple test cases. The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and kk (2n105,1kn2 \le n \le 10^5, 1 \le k \le n) — the number of cows and your cow's index.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \le a_i \le 10^9) — the Cowdeforces rating of the cows. It is guaranteed that aia_i's are pairwise different.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output Format: For each test case, print one integer: the maximum number of wins cow kk can achieve if you choose to swap (or do nothing) optimally.

Note: In the first test case, it is optimal to do nothing. Let aa' be the Cowdeforces rating of the cows in the original order (with your cow's rating bolded), then

  • Initially, a=[12,10,14,11,8,3]a' = [\mathbf{12}, 10, 14, 11, 8, 3].
  • Your cow plays against the cow with Cowdeforces rating 1010 and wins. a=[12,14,11,8,3]a' = [\mathbf{12}, 14, 11, 8, 3].
  • Your cow plays against the cow with Cowdeforces rating 1414 and loses.

In the second test case, it is optimal to swap your cow to position 33. Then, let aa' be the Cowdeforces rating of the cows in the order after the swap.

  • Initially, a=[7,2,12,10,727,13]a' = [7, 2, \mathbf{12}, 10, 727, 13].
  • The cow with Cowdeforces rating 77 plays against the cow with Cowdeforces rating 22 and wins. a=[7,12,10,727,13]a' = [7, \mathbf{12}, 10, 727, 13].
  • The cow with Cowdeforces rating 77 plays against your cow, and your cow wins. a=[12,10,727,13]a' = [\mathbf{12}, 10, 727, 13].
  • Your cow plays against the cow with Cowdeforces rating 1010 and wins. a=[12,727,13]a' = [\mathbf{12}, 727, 13].
  • Your cow plays against the cow with Cowdeforces rating 727727 and loses.

Sample Cases

Case 1

Input

3
6 1
12 10 14 11 8 3
6 5
7 2 727 10 12 13
2 2
1000000000 1

Output

1
2
0

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback