码迷,mamicode.com
首页 > 其他好文 > 详细

CF1251D Salary Changing

时间:2020-02-02 13:52:53      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:Nging   using   pre   hang   c++   while   pair   long   include   

思路:

二分答案。

实现:

 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

标签:Nging   using   pre   hang   c++   while   pair   long   include   

原文地址:https://www.cnblogs.com/wangyiming/p/12251805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!