题意:给了n个人,每个人两个属性l,r。表示能拿工资的范围。boss有s元,问最多可以使n人拿到的工资的中位数最大。
思路:分别根据l,r进行两次排序,求出可以二分的区间,然后对于每个中位数,进行二分。ri小于x的人和li大于x的人拿li元(所以在二分前,要排序让li小的在前面),为了让钱更多给其他的人。对于其他人,按照li从小到大排序。
优先补充x左面的人,即sum += li。补充右面的人只需要sum+=x即可。
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl ' ' #define inf 0x3f3f3f3f; #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl #define P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define ppb() pop_back() #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(a)); using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 2e5+10; ll n,s; struct node { ll l,r; }a[maxn]; bool cmp1(node a,node b) { return a.l > b.l; } bool cmp2(node a,node b) { return a.r > b.r; } ll judge(int x) { std::vector<int>v; ll sum = 0; ll cnt1 = 0,cnt2 = 0; rep(i,1,n){ if(a[i].r < x){ sum += a[i].l; cnt1++; } else if(x < a[i].l){ sum += a[i].l; cnt2++; } else { v.pb(a[i].l); } } sort(v.begin(),v.end()); for(auto i : v){ if(cnt1 < n/2){ sum += i; cnt1++; } else sum += x; } if(sum > s) return 0; else return 1; } int main() { //freopen("input.txt", "r", stdin); int _; scanf("%d",&_); while(_--) { cin>>n>>s; rep(i,1,n) cin>>a[i].l>>a[i].r; sort(a+1,a+1+n,cmp1); ll L = a[n/2+1].l; sort(a+1,a+1+n,cmp2); ll R = a[n/2+1].r; sort(a+1,a+1+n,cmp1); ll ans = 0; //L = 1,R = 1e9; while(L <= R){ ll mid = (L+R)/2; if(judge(mid)){ ans = max(mid,ans); L = mid+1; } else R = mid-1; } cout<<ans<<endl; } }