The meaning of problems
intervals, each interval a number taken from the median as large as the last required
The meaning of problems
The first reaction is two points
but I, two points monotony of understanding, poor
I was rejected, I think about enumeration, then I'm gone.
In fact, half of the median, for a median, must be a part in your left, your right part.
If the left part of more than half, indicating that the median is too big.
If more than half of the right part, illustrate the median is too small.
If under normal circumstances, the only remaining
interval, and this time take the first left point makes the smallest left to take over half, then take the right half full +1, the median size of the contribution is to the right
if this ratio and
small, indicating compliance, then take a larger median.
On the other hand, non-compliance, whichever is smaller median.
In the case of guarantee do not exceed the left and right sides, how do you prove it monotonic?
First, the vast majority of contributions certainly take the left point, to take a small part of the median.
Something like this:
、
ensure that part of the left and right, first of all they will not overrun, and they must take the left point.
、
part is contained in the median part, the left part of the left point to take the right to take the median.
So sort of the original range, must take the first half of the left point.
We consider increasing median, obviously if the prerequisites for the establishment, with median increases, the range will be able to choose more, that is, to become more median.
represents the coordinates of the left and the suffixes
Comparison can be found, the left portion is enlarged, the right part is reduced.
But the right part of the left minus points, all in the left part becomes
, and
must be greater than or equal left endpoint.
and
also becomes
, so
. Monotone.
to sum up
This is the first time with a legal or two points I encountered.
Precisely for illegal situation, apparently to meet the half, it left more than a big explanation, the right more explanation smaller.
In legitimate cases, but also to meet the half, because the left part of the left to take a certain point, the right part with median increases, taking the median changed much, although taken left coordinate reduced, but by then because they are to get the median, so the contribution must be increased.
#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
typedef long long ll;
using namespace std;
const ll mod = 1e9+7;
const int maxn = 2e5+100;
struct node{
int l,r;
friend bool operator < (node a,node b){
if(a.l==b.l)return a.r<b.r;
return a.l<b.l;
}
}A[maxn];
int n;ll s;
bool check(int x){
int lt=0,rt=0;
ll ret=0;
//cout<<x<<endl;
for(int i=1;i<=n;i++){
if(A[i].r<x)lt++,ret+=A[i].l;
else if(A[i].l>x)rt++,ret+=A[i].l;
}
if(rt>=n/2+1)return true;
if(lt>=n/2+1)return false;
for(int i=1;i<=n;i++){
if(A[i].r<x||A[i].l>x)continue;
if(lt<n/2)ret+=A[i].l,lt++;
else ret+=x,rt++;
}
return ret<=s;
}
int main(){
int T;cin>>T;
while(T--){
scanf("%d%lld",&n,&s);
FOR(i,1,n)scanf("%d%d",&A[i].l,&A[i].r);
sort(A+1,A+1+n);
int l=1,r=1e9+1,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){
ans=max(ans,mid);
l=mid+1;
}
else r=mid-1;
}
printf("%d\n",ans);
}
}