CF1251D Salary Changing
Posted wangyiming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1251D Salary Changing相关的知识,希望对你有一定的参考价值。
思路:
二分答案。
实现:
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 }
以上是关于CF1251D Salary Changing的主要内容,如果未能解决你的问题,请参考以下文章
Salary Changing CodeForces - 1251D (二分)
Codeforces 1251D Salary Changing (二分+最大化中位数)