Battle Cows (Codeforces 1951B)
Tags: Greedy, binary search

The HU - Shireg Shiregඞ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:   The first match is between the cow in position $$$1$$$ and the cow in position $$$2$$$.  Subsequently, each match $$$i$$$ is between the cow in position $$$i+1$$$ and the winner of match $$$i-1$$$.  In each match, the cow with the higher Cowdeforces rating wins and proceeds to the next match. 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.Find the maximum number of wins your cow can achieve.

Input: 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.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.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.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: 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.

Note: 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   Initially, $$$a' = [\mathbf{12}, 10, 14, 11, 8, 3]$$$.  Your cow plays against the cow with Cowdeforces rating $$$10$$$ and wins. $$$a' = [\mathbf{12}, 14, 11, 8, 3]$$$.  Your cow plays against the cow with Cowdeforces rating $$$14$$$ and loses.  In total, your cow wins $$$1$$$ match.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.   Initially, $$$a' = [7, 2, \mathbf{12}, 10, 727, 13]$$$.  The cow with Cowdeforces rating $$$7$$$ plays against the cow with Cowdeforces rating $$$2$$$ and wins. $$$a' = [7, \mathbf{12}, 10, 727, 13]$$$.  The cow with Cowdeforces rating $$$7$$$ plays against your cow, and your cow wins. $$$a' = [\mathbf{12}, 10, 727, 13]$$$.  Your cow plays against the cow with Cowdeforces rating $$$10$$$ and wins. $$$a' = [\mathbf{12}, 727, 13]$$$.  Your cow plays against the cow with Cowdeforces rating $$$727$$$ and loses.  In total, your cow wins $$$2$$$ matches.

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

Sample Outputs:
1
2
0

Solution (Python):
def max_win(L, k):
    (i, j) = (0, 0)
    while L[k] > L[i] and i <= k:
        i += 1
    j = i + 1
    while j < len(L) and L[k] > L[j]:
        j += 1
    if i == k and i != 0:
        return j - 1
    if i == 0:
        return j - 1
    elif i != 0:
        return max(i - 1, j - i)
t = int(input())
M = []
for i in range(t):
    (n, k) = input().split()
    L = input().split()
    for j in range(len(L)):
        L[j] = int(L[j])
    k = int(k) - 1
    m = max_win(L, k)
    M.append(m)
[print(num) for num in M]

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, k) = readInts()
    a = readList()
    u = v = -1
    for i in range(n):
        if a[i] > a[k - 1]:
            if u == -1:
                u = i
            elif v == -1:
                v = i
            else:
                break
    ans = max(0, u - 1)
    if u == v == -1:
        ans = n - 1
    elif v == -1:
        if k - 1 > u:
            ans = max(ans, k - 1 - u - 1) if u == 0 else max(ans, k - 1 - u)
    elif k - 1 > v:
        ans = max(ans, v - u - 1) if u == 0 else max(ans, v - u)
    elif k - 1 > u:
        ans = max(ans, k - 1 - u - 1) if u == 0 else max(ans, k - 1 - u)
    return ans
for _ in range(int(input())):
    print(solve())

Solution (Python):
import sys
input = sys.stdin.readline

def battleCows(n, k, z):
    index = 0
    for i in range(k - 1):
        if z[i] > z[k - 1]:
            index = i
            break
    (z[index], z[k - 1]) = (z[k - 1], z[index])
    cnt = 0
    for i in range(index + 1, len(z)):
        if z[i] < z[index]:
            cnt += 1
        else:
            break
    if index != 0:
        cnt += 1
    cnt = max(cnt, index - 1)
    return cnt
t = int(input())
for _ in range(t):
    (n, k) = map(int, input().split())
    z = list(map(int, input().split()))
    print(battleCows(n, k, z))