Level : Codeforces Div-2 D
You are managing a company with n employees, where n is guaranteed to be odd.
You have a total budget of s dollars to distribute as salaries. For every employee i, their salary must be chosen within the range [l_i, r_i].
Your task is to assign a valid salary to every employee such that the median salary is as large as possible.
For a sequence containing an odd number of elements, its median is the element located at the middle position after sorting the sequence in non-decreasing order.
For example:
[5, 1, 10, 17, 6] is 6.[1, 2, 1] is 1.It is guaranteed that the available budget is sufficient to give every employee their minimum possible salary:
l_1 + l_2 + ... + l_n ≤ s
You are not required to use the entire budget.
The first line contains an integer t — the number of test cases.
For each test case:
n and s, representing the number of employees and the total available budget.n lines each contain two integers l_i and r_i, representing the minimum and maximum salary allowed for employee .i1 ≤ t ≤ 2 · 10^51 ≤ n < 2 · 10^5n is odd1 ≤ s ≤ 2 · 10^141 ≤ l_i ≤ r_i ≤ 10^9n over all test cases does not exceed 2 · 10^5Σ l_i ≤ sFor every test case, print a single integer — the maximum median salary that can be achieved while satisfying all salary ranges and staying within the available budget.
3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
11
1337
6
In the first test case, one possible salary assignment is:
[12, 2, 11]
After sorting, it becomes [2, 11, 12], so the median is 11.
In the second test case, there is only one employee, and their salary can be exactly 1337, making the median 1337.
For the third test case, one valid assignment is:
[4, 3, 6, 6, 7]
After sorting, the median is 6.
Therefore, the maximum possible medians for the three test cases are 11, 1337, and 6, respectively.
Titan • Pending