Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 2e5 + 10;
- int n, s;
- int S;
- int ql[N], qr[N];
- vector <pair <int, int>> lr;
- bool ok(int c){
- int cntBigR = 0;
- int cntBigL = 0;
- for (int i = 1; i <= n; i++){
- if (qr[i] >= c){
- cntBigR++;
- }
- if (ql[i] > c){
- cntBigL++;
- }
- }
- if (cntBigR < (n + 1) / 2) return false;
- assert(cntBigL < (n + 1) / 2);
- long long sum = 0;
- int needL = (n + 1);
- int need = needL / 2;
- for (int i = n - 1; i >= 0; i--){
- if (need == 0) break;
- if (lr[i].second < c) continue;
- need--;
- if (lr[i].first <= c) sum += c - lr[i].first;
- }
- return sum <= s;
- }
- void solve(){
- cin >> n >> s;
- for (int i = 1; i <= n; i++){
- cin >> ql[i] >> qr[i];
- }
- for (int i = 1; i <= n; i++){
- s -= ql[i];
- lr.push_back({ql[i], qr[i]});
- }
- sort(lr.begin(), lr.end());
- assert(s >= 0);
- sort(ql + 1, ql + n + 1);
- sort(qr + 1, qr + n + 1);
- int l = ql[(n + 1) / 2];
- int r = 1e9 + 10;
- int ans = 0;
- while (l <= r){
- int mid = (l + r) >> 1;
- if (ok(mid)){
- l = mid + 1;
- ans = mid;
- }else{
- r = mid - 1;
- }
- }
- cout << ans << endl;
- }
- int main(){
- ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement