2020/4/29
1.二分
题面
You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).
You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from li to ri dollars. You have to distribute salaries in such a way that the median salary is maximum possible.
To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:
the median of the sequence [5,1,10,17,6] is 6,
the median of the sequence [1,2,1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l1+l2+⋯+ln≤s.
Note that you don’t have to spend all your s dollars on salaries.
You have to answer t test cases.
输入
The first line contains one integer t (1≤t≤2⋅105) — the number of test cases.
The first line of each query contains two integers n and s (1≤n<2⋅105, 1≤s≤2⋅1014) — the number of employees and the amount of money you have. The value n is not divisible by 2.
The following n lines of each query contain the information about employees. The i-th line contains two integers li and ri (1≤li≤ri≤109).
It is guaranteed that the sum of all n over all queries does not exceed 2⋅105.
It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. ∑i=1nli≤s.
输出
For each test case print one integer — the maximum median salary that you can obtain.
样例
输入
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
题意就是你有n (n为奇数) 个员工,s单位的钱。每个员工都要分配给其[l,r]区间中的钱数,让所有员工中位数的钱数最大化。
最近搞点二分题水一水,这题就是二分。只要使大于等于mid的人多于n/2+1即可,至于另一半小于的其实可以不必保证,因为他会继续往更大的去check,所以即使是0也是可以的(即使他不可以)。
bool check(ll x, ll s, vector<pll>& a) {
int cnt = 0;
for (int i = a.size() - 1; i >= 0; --i) {
if (a[i].second >= x && cnt <= a.size() / 2) {
cnt++;
s -= max(x, a[i].first);
} else {
s -= a[i].first;
}
}
return s >= 0 && cnt > a.size() / 2;
}