-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathd.cpp
77 lines (69 loc) · 1.83 KB
/
d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
template <typename T1, typename T2>
ostream &operator <<(ostream &os, pair<T1, T2> p){os << p.first << " " << p.second; return os;}
template <typename T>
ostream &operator <<(ostream &os, vector<T> &v){for(T i : v)os << i << ", "; return os;}
template <typename T>
ostream &operator <<(ostream &os, set<T> s){for(T i : s) os << i << ", "; return os;}
template <typename T1, typename T2>
ostream &operator <<(ostream &os, map<T1, T2> m){for(pair<T1, T2> i : m) os << i << endl; return os;}
ll ans = 0;
pii lr[200000];
bool cmp(pii a, pii b){
return (pii(a.second, a.first) < pii(b.second, b.first));
}
bool works(ll N, ll S, ll mid){
ll start = 0;
sort(lr, lr+N, cmp);
for(; start < N; start++){
if(lr[start].second >= mid){
break;
}
S -= lr[start].first;
}
if(N - start < (N+1)/2){
return false;
}
sort(lr+start, lr+N);
for(ll i = start; N - i > (N+1)/2; i++){
S -= lr[i].first;
}
for(ll i = (N+1)/2-1; i < N; i++){
S -= max(mid, lr[i].first);
}
return (S >= 0);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll T; cin >> T;
for(ll tc = 1; tc <= T; tc++){
ll N, S; cin >> N >> S;
for(ll i = 0; i <N; i++){
cin >> lr[i].first >> lr[i].second;
}
ll lo = 0;
ll hi = 1e9;
ll mid = 0;
while(lo < hi){
mid = (lo+hi)/2;
if(works(N, S, mid)){
lo = mid+1;
}else{
hi = mid-1;
}
}
while(works(N, S, mid)){
mid++;
}
while(!works(N, S, mid)){
mid--;
}
cout << mid << endl;
}
return 0;
}