传送门
文章目录
- 题意:
- 思路:
题意:
思路:
直接算不好算,考虑二分这个中位数midmidmid。
考虑如何checkcheckcheck,这个分情况来就好了:
(1)mid>a[i].r(1)mid>a[i].r(1)mid>a[i].r,这个时候这个人永远不能到midmidmid,所以给他a[i].la[i].la[i].l。
(2)mid<a[i].l(2)mid<a[i].l(2)mid<a[i].l,这个时候给多少都>mid>mid>mid,所以给他a[i].la[i].la[i].l。
(3)a[i].l≤mid≤a[i].r(3)a[i].l\le mid\le a[i].r(3)a[i].l≤mid≤a[i].r,这个时候只需要找出几个<mid,≥mid<mid,\ge mid<mid,≥mid即可,将a[i].la[i].la[i].l从小到大排序贪心来选即可。
// Problem: D. Salary Changing
// Contest: Codeforces - Educational Codeforces Round 75 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1251/problem/D
// Memory Limit: 256 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
LL s;
struct Node {int l,r;bool operator < (const Node &W) const {return l<W.l;}
}a[N];
vector<Node>v;bool check(int mid) {v.clear();LL now=s;int cnt1,cnt2; cnt1=cnt2=0;for(int i=1;i<=n;i++) {if(a[i].r<mid) cnt1++,now-=a[i].l;else if(a[i].l>mid) cnt2++,now-=a[i].l;else v.pb(a[i]);}if(cnt1>n/2) return false;for(int i=0;i<v.size();i++) {if(cnt1<n/2) {cnt1++;now-=v[i].l;} else if(cnt2<n/2+1) {cnt2++;now-=mid;}}return now>=0;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int _; cin>>_;while(_--) {scanf("%d%lld",&n,&s);for(int i=1;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].r);sort(a+1,a+1+n);int l=1,r=1e9,ans;while(l<=r) {int mid=(l+r)>>1;if(check(mid)) ans=mid,l=mid+1;else r=mid-1;}printf("%d\n",ans);}return 0;
}
/**/