Salary Changing CF-1251D(二分)

题意:

有$n$个员工,$s$元钱,现在要给每个员工发工资。每个员工的工资的范围$(l_i,r_i)$,求所有员工的工资中位数的最大值。

思路:

二分答案,$check$的时候判断工资可以大于等于$mid$的员工个数,用最小代价购买之后判断总价钱会不会超出范围。

代码:

 1 //#include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13
14 #define ll long long
15 #define pll pair<ll,ll>
16 #define pii pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 #define pb push_back
24
25 using namespace std;
26 const int inf = 2e9 + 7;
27 const ll Inf = 1e18 + 7;
28 const int maxn = 2e5 + 5;
29 const int mod = 1e9 + 7;
30
31 struct node
32 {
33     ll l, r;
34 }p[maxn];
35
36 ll s, n;
37
38 bool cmp(const node& a, const node& b)
39 {
40     return a.l < b.l;
41 }
42
43 bool check(ll x)
44 {
45     ll res = 0, m = (n + 1) >> 1;
46     for (int i = n; i >= 1; --i)
47     {
48         if (m && p[i].l <= x && p[i].r >= x)
49         {
50             res += x;
51             m--;
52         }
53         else
54         {
55             res += p[i].l;
56             if (p[i].l >= x && m)    m--;
57         }
58     }
59     if (m || res > s)    return 0;
60     return 1;
61 }
62
63
64 int main()
65 {
66     int T;
67     scanf("%d", &T);
68     while (T--)
69     {
70         scanf("%lld %lld", &n, &s);
71         for (int i = 1; i <= n; ++i)
72         {
73             scanf("%lld %lld", &p[i].l, &p[i].r);
74         }
75         sort(p + 1, p + 1 + n, cmp);
76         ll l = 0, r = s;
77         while (l <= r)
78         {
79             ll mid = (l + r) >> 1;
80             if (check(mid))    l = mid + 1;
81             else r = mid - 1;
82         }
83         printf("%lld\n", l - 1);
84     }
85 }

原文地址:https://www.cnblogs.com/zhang-Kelly/p/12693339.html

时间: 2024-11-14 00:08:38

Salary Changing CF-1251D(二分)的相关文章

二分题 D - Salary Changing codeforce

题意:给出n个人(n是奇数),s钱:s为总的可以付工钱的钱: 每一个工人有一个付工钱的区间,只要在这个区间范围内,随便一个数都可以当作给这个工人付了钱: 老板要付给每个工人钱,并且付钱的中位数要尽可能大: 问:最大的中位数是多少: 思路:贪心+思维+二分: 我们以中位数为主体进行二分.那么就需要n/2+1个大于等于中位数的数: 这个时候我们先给钱排序,按第一个数从大到小排: 然后check部分,从1到n遍历,如果满足x在区间范围内,就取x这个数: 那么,为什么就要取这个数呢,因为我们迟早要凑到n

Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing

链接: https://codeforces.com/contest/1251/problem/D 题意: You are the head of a large enterprise. n people work at you, and n is odd (i.?e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it,

CF#609E|二分+树状数组

队友发了一道cf的题过来,然后..一上午就做了一道题.. CF#609E 题目地址 复习树状数组求逆序数1 复习树状数组求逆序数2 参考博客1 参考博客2 题目大意:每次可以移动相邻的结点,求最小能够出现1~k子序列的交换次数 思路: 最小交换次数,首先想到与逆序数有关,以前做过类似的题,3 2 1,交换成 1 2 3的最小次数,就是求 3 2 1这个序列的逆序数=3 这题稍微有点变化,就是3 2 1 中间可能还存在 其它数字,比如 3 4 5 2 1,要我们求 出现 3 2 1 的最小交换次数

D. Salary Changing(找中位数)

题:https://codeforces.com/contest/1251/problem/D 题意:给你n个单位需要满足达到的区间,再给个s,s是要分配给n的单位的量,当然∑l<=s,问经过分配后能够达到的最大中位数是多少 题解:二分找中位数,成立原因:代码注释 #include<bits/stdc++.h> using namespace std; typedef long long ll; #define fo(i,a,b) for(int i=a;i<=b;i++) #de

codeforces D Salary Changing

题意:给你n个人,和s块钱,每个人都有一个工资区间,你给所有人都发工资.然后要他们工资的中位数最大. 思路:二分找那个值.那个值要满足至少有n/2+1个工资区间内. #include<cstdio> #include<cstring> #include<queue> #include<cmath> #include<algorithm> #include<map> #include<vector> #include<

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 (

Educational Codeforces Round 75

目录 Contest Info Solutions A. Broken Keyboard B. Binary Palindromes C. Minimize The Integer D. Salary Changing E2. Voting (Hard Version) Contest Info Practice Link Solved A B C D E1 E2 F 6/7 O O O O O O - O 在比赛中通过 ? 赛后通过 ! 尝试了但是失败了 - 没有尝试 Solutions A.

Educational Codeforces Round 75 ABCD题解

A. Broken Keyboard Description 给出一串小写字母字符序列,连续出现两次的字母为坏掉的,按字典序输出所有没有坏掉的字母. Solution 模拟暴力删除字母,注意相同字母的去重. 1 #include <algorithm> 2 #include <cctype> 3 #include <cmath> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cst

CF #262 (DIV2) C . Present (二分答案)

output standard output Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on