思路:
二分答案。
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N = 200005; 5 ll l[N], r[N]; 6 bool check(ll x, int n, ll s) 7 { 8 ll tmp = 0; 9 int low = 0, up = 0; 10 vector<pair<ll, ll>> v; 11 for (int i = 0; i < n; i++) 12 { 13 if (r[i] < x) { low++; tmp += l[i]; } 14 else if (l[i] >= x) { up++; tmp += l[i]; } 15 else v.push_back(make_pair(l[i], r[i])); 16 } 17 if (up >= (n + 1) / 2) return true; 18 int r = (n + 1) / 2 - up; 19 if (v.size() < r) return false; 20 sort(v.begin(), v.end()); 21 for (int i = 0; i < v.size() - r; i++) tmp += v[i].first; 22 tmp += r * x; 23 return tmp <= s; 24 } 25 int main() 26 { 27 int t; cin >> t; 28 while (t--) 29 { 30 int n; ll s; cin >> n >> s; 31 ll L = 0, R = 0; 32 for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; R = max(R, r[i]); } 33 int res = 0; 34 while (L <= R) 35 { 36 ll m = L + R >> 1; 37 if (check(m, n, s)) { res = m; L = m + 1; } 38 else R = m - 1; 39 } 40 cout << res << endl; 41 } 42 return 0; 43 }
原文地址:https://www.cnblogs.com/wangyiming/p/12251805.html
时间: 2024-11-14 00:08:44