链接:

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. 修改Linux服务器中的MySql密码

    1.可以直接在数据库中修改,因为知道root密码,所以直接登录 mysql -uroot -p 2.查看一下数据库,修改root密码需要使用如下图所示的mysql数据库 3.通过use mysql指明 ...

  2. flask返回数据类型

    服务器这种后台返回的数据只能是string,json或者是文件类型,对应Html的解析文件类型 无法返回自定义的元组,只能返回规定好的元组,说白了只第一个元素有效 所有返回前台的内容其实都应该是Res ...

  3. Zuul【基础配置】

    概述:zuul底层是基于servlet,是由一系列的filter链构成. 1.路由配置 a.单例serverId映射 zuul: routes: client-a: path: /client/** ...

  4. 03 python 对象笔记

    类的命名方法 1.使用大驼峰命名法:每一个单词的首字母大写(第一个的也要)2.单词之间不需要下划线 对象的内置函数和属性 1.使用dir()函数来获取对象的内置方法和属性.返回值是一个列表.2.返回中 ...

  5. MRR,BKA,ICP相关

    MRR Multi-Range Read,多范围读,5.6以上版本开始支持 工作原理&优化效果: 将查询到的辅助索引结果放在一个缓冲(read_rnd_buffer_size = 4M)中 将 ...

  6. Scala 面向对象编程之对象

    此对象非彼java bean对象 是scala object的对象 Object // object,相当于class的单个实例,通常在里面放一些静态的field或者method // 第一次调用ob ...

  7. Yii2 redis 使用方法

    /** * 基于 yii2.0 redis使用方法 *///项目根目录命令行执行 composer require --prefer-dist yiisoft/yii2-redis; //在配置文件中 ...

  8. MySql数据库操作之数据约束

    首先数据库的外键是数据库提供的一种完整性约束.在许多数据库的书上也会介绍到,然而对于外键这个完整性性约束究竟应该在数据库端实现,还是在项目业务端实现很多人有着不同的意见. 个人开发(小型应用).数据库 ...

  9. Feign报错'xx.FeignClientSpecification', defined in null, could not be registered.

    解决: 在application.yml中配置: spring: main: allow-bean-definition-overriding: true 参考:https://blog.csdn.n ...

  10. MySql 学习之 一条更新sql的执行过程

    上一篇文章咱们说了一条查询sql的执行过程.如果没有看过上一篇文章的可以去看下上一篇文章,今天咱们说说一条更新sql的执行过程. 上面一条sql是将id为1的分数加上10. 那么它的执行流程是怎样的呢 ...