E1. Submedians (Easy Version)
Problem Statement
This is the easy version of the problem. The only difference is that in this version, you are asked to find a subarray only for the maximum submedian.
You can make hacks only if both versions of the problem are solved.
Definition
An integer is a median of an array of length if and only if:
- is greater than or equal to at least elements of the array, and
- is less than or equal to at least elements of the array.
Examples
- The only median of is .
- The medians of are , , and .
- The only median of is .
Task
You’re given an integer and an array of integers between and .
An integer from to is said to be a submedian if there exists at least one pair of indices such that:
- is a median of the subarray
It can be proven that there always exists at least one submedian. Find the maximum submedian and any corresponding pair of indices .
Input
Each test contains multiple test cases.
The first line contains the number of test cases ().
The description of the test cases follows.
- The first line of each test case contains two integers and ().
- The second line of each test case contains integers ().
It is guaranteed that the sum of over all test cases doesn’t exceed .
Output
For each test case, output three integers , , and — the maximum submedian and the bounds of a subarray of length at least () such that is one of its medians.
If there are many solutions, you can print any of them.
Solution
The problem asks for the maximum submedian v_max. This structure suggests that we can binary search on the answer. We need to find a monotonic predicate check(x) to guide the binary search.
Let’s define check(x) as: “Does there exist a subarray a[l...r] with length m = r-l+1 >= k such that the number of elements greater than or equal to x is at least the number of elements less than x?”.
The binary search will find the largest integer v_ans for which check(v_ans) is true. We will prove that this v_ans is the maximum submedian.
The check(x) Function
To implement check(x) efficiently, we transform the array a into a helper array b:
b[i] = 1ifa[i] >= xb[i] = -1ifa[i] < x
The condition “count of elements >= x >= count of elements < x” in a subarray a[l...r] is equivalent to the sum of corresponding elements in b being non-negative: sum(b[l...r]) >= 0.
We can find if such a subarray exists in time using prefix sums. Let P be the prefix sum array of b (P[i] = sum(b[1...i])). We need to find l, r with r-l+1 >= k such that P[r] - P[l-1] >= 0, or P[r] >= P[l-1].
For each r from k to n, we check if P[r] >= min(P[0], P[1], ..., P[r-k]). This check can be done by iterating r from k to n while maintaining the minimum prefix sum in the required window.
Proof of Correctness
Let v_ans be the value returned by our binary search. We need to prove v_ans is the true maximum submedian, v_max.
v_max <= v_ans: Letv_maxbe the true maximum submedian. By definition, there is a subarrayA'of lengthmfor whichv_maxis a median. Forv_maxto be a median ofA', the count of elements>= v_maxmust be at least . This implies the count of elements>= v_maxis at least the count of elements< v_max. Therefore,check(v_max)is true. Since our binary search finds the largest valuev_ansfor whichcheckis true, we must havev_ans >= v_max.v_ans <= v_max: This requires showing thatv_ansis a submedian. By the definition of our binary search,check(v_ans)is true andcheck(v_ans + 1)is false. Sincecheck(v_ans)is true, there exists a subarrayA'of lengthmwhere:p = count(a_i >= v_ans)q = count(a_i < v_ans)p >= q
We need to show that
v_ansis a median of this subarrayA'. The median definition requires two conditions: a)count(a_i >= v_ans) >= \lceil m/2 \rceil: This isp >= \lceil m/2 \rceil. Sincep >= qandp+q=m, this is always true. b)count(a_i <= v_ans) >= \lceil m/2 \rceil: This is what we need to prove. Letp_eqbe the count of elements equal tov_ans. The condition isq + p_eq >= \lceil m/2 \rceil.Let’s use the fact that
check(v_ans + 1)is false. For our specific subarrayA', this means the count of elements>= v_ans + 1is less than the count of elements< v_ans + 1.count(a_i >= v_ans + 1)isp - p_eq.count(a_i < v_ans + 1)isq + p_eq.- So, we have the inequality:
p - p_eq < q + p_eq, which simplifies top < q + 2*p_eq.
Assume for contradiction that our condition (b) is false:
q + p_eq < \lceil m/2 \rceil. This meansq + p_eq <= \lceil m/2 \rceil - 1. Sincem = p+q, we havep = m-q. Substitute this into the inequality fromcheck(v_ans+1):m - q < q + 2*p_eq \implies m < 2q + 2*p_eq \implies m/2 < q + p_eq. Combining our assumption and this result, we get:m/2 < q + p_eq \le \lceil m/2 \rceil - 1. This inequality can never be true for any integerm. For example, ifm=2k, it becomesk < q+p_eq \le k-1, a contradiction. Ifm=2k+1, it becomesk+0.5 < q+p_eq \le k, also a contradiction. Therefore, our assumption must be false. Condition (b)q + p_eq >= \lceil m/2 \rceilmust be true.Since both median conditions hold for
v_ansin subarrayA',v_ansis a submedian. This impliesv_ans <= v_max.
Combining both parts, we have v_ans = v_max. The algorithm is correct.