链接:

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;
}

最新文章

  1. Java 周历日历
  2. VMware 不可恢复错误(svga)”解决方法
  3. Struts2中使用Servlet API步骤
  4. Json.NET 利用ContractResolver解决命名不一致问题
  5. mongodb 入门笔记
  6. 【原】window上安装elasticserach
  7. 集成支付宝报一堆warning: (arm64) /Users/scmbuild/workspace/standard-pay/.....警告问题解决办法亲测可行!
  8. Homebrew新一代OS X套件管理工具 高速安装Git
  9. Web UI 网站用户界面设计命名规范
  10. 如何上传base64编码图片到七牛云
  11. Systrace
  12. Linux 常 用 命 令
  13. 【iOS】Swift LAZY 修饰符和 LAZY 方法
  14. Integer Replacement
  15. ASP.NET后台输出js
  16. MySQL报1130错误解决办法
  17. PAT-GPLT训练集 L1-043 阅览室
  18. 2018.10.26 NOIP模拟 图(最小生成树+线段树合并)
  19. MySQL排序:SELECT ORDER BY
  20. loadrunner和QTP视频教程汇总

热门文章

  1. 线性表——顺序表的实现与讲解(C++描述)
  2. linux 加载新的磁盘(卷组)
  3. Ribbon【自定义客户端】
  4. mininet安装配置
  5. iTunes向ipad传影片
  6. Kubernetes 学习笔记(二):本地部署一个 kubernetes 集群
  7. VS.NET(C#)--2.3良构的XHTML
  8. nginx关闭日志功能access_log关闭
  9. c#的异步处理思路和vue前端中异步处理思路比较
  10. 关于Vue中,使用watch同时监听两个值的实现方法