Salary Changing CodeForces - 1251D(二分+贪心最大化中位数)

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.

Input
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.

Output
For each test case print one integer — the maximum median salary that you can obtain.

Example
Input
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
Output
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal1=12,sal2=2,sal3=11 (sali is the salary of the i-th employee). Then the median salary is 11.

In the second test case, you have to pay 1337 dollars to the only employee.

In the third test case, you can distribute salaries as follows: sal1=4,sal2=3,sal3=6,sal4=6,sal5=7. Then the median salary is 6.
思路:题目要求最大化中位数,假设当前中位数为x,我们只需要保证有n/2+1个位置包含x就可以了。按照左端点由小到大排序,然后倒序去找(这样我们可以优先考虑>=x的数)。同时需要判断最小价值是否大于s以及是否有n/2+1个数>=x.
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e5+100;
struct node{
	ll l,r;
	bool operator<(const node &a)const{
		if(l==a.l) return r<a.r;
		else return l<a.l;
	}
}p[maxx];
int n;ll s;

inline bool check(ll x)
{
	ll sum=0;
	int cnt=0;
	for(int i=n;i>=1;i--)
	{
		if(p[i].r>=x&&cnt*2<n)
		{
			if(p[i].l<=x) sum+=x;
			else sum+=p[i].l;
			cnt++;
		}
		else sum+=p[i].l;//上面的条件满足了,就直接加最小值就可以了。
	}
	return sum<=s&&cnt*2>n;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%lld",&n,&s);
		for(int i=1;i<=n;i++) scanf("%lld%lld",&p[i].l,&p[i].r);
		sort(p+1,p+1+n);
		ll l=0,r=s,mid,ans;
		while(l<=r)
		{
			mid=l+r>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		cout<<ans<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值