题意:n(n为奇数)个员工,每个员工的薪水在li-ri之间,一共有s元,使得员工薪水中位数最大。
二分...
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int l[200005],r[200005];
ll s1;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;ll s;
cin>>n>>s;
int h=(n+1)/2;
s1=0;
int ans;
for(int i=0;i<n;i++)cin>>l[i]>>r[i],s1+=l[i];
int l1=1,r1=1e9;
while(l1<=r1)
{
int mid=(l1+r1)>>1;
vector<int>v;
for(int i=0;i<n;i++)
{
if(r[i]>=mid)
v.push_back(max(0,mid-l[i]));
}
sort(v.begin(),v.end());
if(v.size()<h)r1=mid-1;
else
{
ll s2=accumulate(v.begin(),v.begin()+h,0ll);
//cout<<mid<<" "<<s1<<" "<<s2<<endl;
if(s1+s2<=s)ans=mid,l1=mid+1;
else r1=mid-1;
}
}
cout<<ans<<endl;
}
}