Advertisement
miglss

6

Mar 21st, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 2e5 + 10;
  5. int n, s;
  6. int S;
  7. int ql[N], qr[N];
  8. vector <pair <int, int>> lr;
  9.  
  10.  
  11. bool ok(int c){
  12. int cntBigR = 0;
  13. int cntBigL = 0;
  14. for (int i = 1; i <= n; i++){
  15. if (qr[i] >= c){
  16. cntBigR++;
  17. }
  18. if (ql[i] > c){
  19. cntBigL++;
  20. }
  21. }
  22. if (cntBigR < (n + 1) / 2) return false;
  23. assert(cntBigL < (n + 1) / 2);
  24. long long sum = 0;
  25. int needL = (n + 1);
  26. int need = needL / 2;
  27. for (int i = n - 1; i >= 0; i--){
  28. if (need == 0) break;
  29. if (lr[i].second < c) continue;
  30. need--;
  31. if (lr[i].first <= c) sum += c - lr[i].first;
  32. }
  33. return sum <= s;
  34. }
  35.  
  36. void solve(){
  37. cin >> n >> s;
  38. for (int i = 1; i <= n; i++){
  39. cin >> ql[i] >> qr[i];
  40. }
  41. for (int i = 1; i <= n; i++){
  42. s -= ql[i];
  43. lr.push_back({ql[i], qr[i]});
  44. }
  45. sort(lr.begin(), lr.end());
  46. assert(s >= 0);
  47. sort(ql + 1, ql + n + 1);
  48. sort(qr + 1, qr + n + 1);
  49. int l = ql[(n + 1) / 2];
  50. int r = 1e9 + 10;
  51. int ans = 0;
  52. while (l <= r){
  53. int mid = (l + r) >> 1;
  54. if (ok(mid)){
  55. l = mid + 1;
  56. ans = mid;
  57. }else{
  58. r = mid - 1;
  59. }
  60. }
  61. cout << ans << endl;
  62. }
  63.  
  64. int main(){
  65.  
  66. ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  67. solve();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement