#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
pair<int,int>a[200007];
int n;
long long s;
bool check(int x){
int num=0;
long long sum=s;
for(int i=n;i;--i){
if(a[i].first>=x)
++num;
else if(a[i].second>=x&&sum>=x-a[i].first){
++num;
sum-=x-a[i].first;
}
}
if(num>=n/2+1)
return 1;
return 0;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
cin>>n>>s;
int x,y;
for(int i=1;i<=n;++i){
cin>>x>>y;
a[i]={x,y};
s-=x;
}
sort(a+1,a+1+n);
int l=a[n/2+1].first,r=1e9;
int ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
cout<<ans<<"\n";
}
return 0;
}
原文地址:https://www.cnblogs.com/ldudxy/p/11832569.html