链接:

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, and the i-th employee should get a salary from li to ri dollars. You have to distribute salaries in such a way that the median salary is maximum possible.

To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:

the median of the sequence [5,1,10,17,6] is 6,

the median of the sequence [1,2,1] is 1.

It is guaranteed that you have enough money to pay the minimum salary, i.e l1+l2+⋯+ln≤s.

Note that you don't have to spend all your s dollars on salaries.

You have to answer t test cases.

思路:

二分, 判断赛的时候贪心判断。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL; const int MAXN = 2e5+10; struct Node
{
int l, r;
bool operator < (const Node& rhs) const
{
if (this->l != rhs.l)
return this->l > rhs.l;
return this->r > rhs.r;
}
}node[MAXN];
int n;
LL s; bool Check(LL val)
{
int p = n/2+1;
LL sum = 0;
for (int i = 1;i <= n;i++)
{
if (node[i].r >= val && p > 0)
{
sum += max(val, (LL)node[i].l);
p--;
}
else
{
sum += node[i].l;
}
}
return (sum <= s && p == 0);
} int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
cin >> n >> s;
for (int i = 1;i <= n;i++)
cin >> node[i].l >> node[i].r;
sort(node+1, node+1+n);
LL l = 1, r = s;
LL res = 0;
while(l <= r)
{
LL mid = (l+r)/2;
//cout << mid << endl;
if (Check(mid))
{
res = max(res, mid);
l = mid+1;
}
else
r = mid-1;
}
cout << res << endl;
} return 0;
}

Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing的更多相关文章

  1. Educational Codeforces Round 75 (Rated for Div. 2)

    知识普及: Educational使用拓展ACM赛制,没有现场hack,比赛后有12h的全网hack时间. rank按通过题数排名,若通过题数相等则按罚时排名. (罚时计算方式:第一次通过每题的时间之 ...

  2. Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer

    链接: https://codeforces.com/contest/1251/problem/C 题意: You are given a huge integer a consisting of n ...

  3. Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes

    链接: https://codeforces.com/contest/1251/problem/B 题意: A palindrome is a string t which reads the sam ...

  4. Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard

    链接: https://codeforces.com/contest/1251/problem/A 题意: Recently Polycarp noticed that some of the but ...

  5. Educational Codeforces Round 75 (Rated for Div. 2)D(二分)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;pair<int,int>a[20 ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  7. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  8. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  9. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

随机推荐

  1. hexo 个人博客搭建

    Hexo 小插曲介绍 虽然标题是第一次写博客. 但是我这个困难户至少挣扎了1年多了, 一直下不去手.今天可算是开了个头. 贵在坚持吧 抽时间介绍我的hexo安装历程吧,今天实在是有点困了,要睡觉了. ...

  2. C++ 用 new 生成一个动态二维数组

    //Microsoft Visual Studio 2015 Enterprise //变长二维数组 #include <iostream> #include<iomanip> ...

  3. C之指针加减运算

    法则:1.指针减指针,语法正确,结果得一个整型值,表示两数值之间的对象类型的空间距离,而不是对象之间的字节数差值 2.指针加指针,语法错误,     3.指针加整形值,语法正确,表示后移N个空间单位 ...

  4. Linux基础-01-Linux基础命令

    1. Linux命令的格式 1) Linux命令的语法格式: 命令 [选项] [参数] 2) 命令格式中命令.选项.参数的具体含义 a) 命令:告诉Linux(UNIX)操作系统做(执行)什么. b) ...

  5. CH08 QSPI启动并从EMMC运行APP

    8.1 概述 在前一节课,我们必须手动挂载TF卡到mnt,然后输入./a.out程序才能启动.而在嵌入式系统里面,我们很多时候需要实现开机启动程序.很多时候我们会把程序固化到FLASH,然后从EMMC ...

  6. dotnet Core学习之旅(序)

    .NET Core 新玩具,新工具,新生产力 我将在博客园我从0.1开始探索.NET Core 的过程. 为什么是从0.1开始而不是从0开始 我在微软刚宣布开源的时候便接触到了.NET Core 1. ...

  7. Sonya and Matrix Beauty CodeForces - 1080E (manacher)

    大意: 给定$nm$字符串矩阵. 若一个子矩形每一行重排后可以满足每行每列都是回文, 那么它为好矩形. 求所有好矩形个数. 一个矩形合法等价于每一行出现次数为奇数的最多只有一个字符, 并且对称的两行对 ...

  8. js new到底做了什么?如何重写new?(转)

    转自:https://blog.csdn.net/lyt_angularjs/article/details/86623988

  9. AJAX中错误代码解释以及http中的错误代码解释

    xmlhttp.readyState的值及含义: 0:请求未初始化(还没有调用 open()). 1:请求已经建立,但是还没有发送(还没有调用 send()). 2:请求已发送,正在处理中(通常现在可 ...

  10. redis重命名flushall和flushdb重启失败

    redis重命名flushall和flushdb,将使用中的redis重命名redis会启动失败并且log中有报错 # Unknown command 'flushall' reading the a ...