Codeforces Round #321 (Div. 2) B 二分+预处理
2 seconds
256 megabytes
standard input
standard output
Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.
Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!
The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105, ) — the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.
Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively.
Print the maximum total friendship factir that can be reached.
4 5
75 5
0 100
150 20
75 1
100
5 100
0 7
11 32
99 10
46 8
87 54
111
In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.
In the second sample test we can take all the friends.
题意:n个朋友 每个朋友拥有mon钱数 fri 友谊值
现在邀请若干朋友参加聚会 要求朋友间的钱数差小于d
输出被邀请参加聚会的 朋友的 友谊值的总和的最大值
题解:sort一下 记录前缀和 转换为取连续区间的和的最大值
固定左边界 按照 题目对钱数差距的要求 二分右边界 最后取区间和的max
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll __int64
using namespace std;
ll n,d;
ll sum[];
struct node
{
ll mon;
ll fri;
}N[];
bool cmp(struct node aa,struct node bb)
{
if(aa.mon<bb.mon)
return true;
return false;
}
int main()
{
scanf("%I64d %I64d",&n,&d);
for(int i=;i<=n;i++)
scanf("%I64d %I64d",&N[i].mon,&N[i].fri);
sort(N+,N++n,cmp);
sum[]=;
ll ans=;
for(int i=;i<=n;i++)
sum[i]=sum[i-]+N[i].fri;
for(int i=;i<=n;i++)
{
ll l=i,r=n,mid;
while(l<r)
{ mid=(l+r+)>>; if((N[mid].mon-N[i].mon)>=d)
r=mid-;
else
l=mid;
}
//cout<<i<<" "<<l<<" "<<mid<<" "<<r<<"&&&"<<endl;
ans=max(ans,sum[l]-sum[i-]);
}
cout<<ans<<endl;
return ;
}
Codeforces Round #321 (Div. 2) B 二分+预处理的更多相关文章
- Codeforces Round #404 (Div. 2) C 二分查找
Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18) 找到 1) [n<= m] cout<<n; 2) ...
- Codeforces Round #321 (Div. 2) Kefa and Company 二分
原题链接:http://codeforces.com/contest/580/problem/B 题意: 给你一个集合,集合中的每个元素有两个属性,$m_i,s_i$,让你求个子集合,使得集合中的最大 ...
- Codeforces Round #321 (Div. 2) B. Kefa and Company 二分
B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...
- Codeforces Round #324 (Div. 2) C (二分)
题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...
- Codeforces Round #377 (Div. 2)D(二分)
题目链接:http://codeforces.com/contest/732/problem/D 题意: 在m天中要考k个课程, 数组a中有m个元素,表示第a[i]表示第i天可以进行哪门考试,若a[i ...
- 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)
题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...
- Codeforces Round #364 (Div. 2) C 二分处理+求区间不同字符的个数 尺取法
C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces Round #551 (Div. 2) E 二分 + 交互
https://codeforces.com/contest/1153/problem/E 题意 边长为n的正方形里面有一条蛇,每次可以询问一个矩形,然后会告诉你蛇身和矩形相交有几部分,你需要在最多2 ...
- Codeforces Round #350 (Div. 2) D2 二分
五一期间和然然打的团队赛..那时候用然然的号打一场掉一场...七出四..D1是个数据规模较小的题 写了一个暴力过了 面对数据如此大的D2无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多 ...
随机推荐
- DOS当中的基本操作命令,如何切换磁盘,如何查看文件和文件夹,如何清屏,进入文件夹的命令,javac是什么意思,作用是什么?DOS如何建文件夹?退出文件夹?
如何切换磁盘:使用盘符+:举例 d: 如何查看文件和文件夹 dir/w 如何清屏: cls (clear screen) 进入文件夹的命令cd ,举例cd JDK javac是什么意思,c 是comp ...
- runtime运行时,类、对象、isa指针
先查看一段OC源码,关于类的定义: /// An opaque type that represents an Objective-C class. typedef struct objc_class ...
- RabbitMQ-消费者"未处理完的消息"丢失
一个关于客户端(消费者)开启自动应答,重启后"未处理消息丢失"的小坑.(主要是对RabbitMQ理解不够) 首先,申明一下: 本文所谓的 "丢失消息" 不是指服 ...
- 爬虫 xpath etree自动补全页面
aa = etree.HTML(response.content) bb = etree.tostring(aa) doc = etree.HTML(bb)
- HTTP-点开浏览器输入网址背后发生的那点事
前言 Internet最早来源于美国国防部ARPANet,1969年投入运行,到现在已有很长一段路了,各位想要了解发展史可以百度下,这里就不多说了. 现如今当我们想要获取一些资料,首先是打开某个浏览器 ...
- Linux系统kernel参数优化
目录 iptables相关 单进程最大打开文件数限制 内核TCP参数方面 内核其他TCP参数说明 众所周知在默认参数情况下Linux对高并发支持并不好,主要受限于单进程最大打开文件数限制.内核TCP参 ...
- 微信小程序 onLoad 函数
小程序注册完成后,加载页面,触发onLoad方法. 页面载入后触发onShow方法,显示页面. 首次显示页面,会触发onReady方法,渲染页面元素和样式,一个页面只会调用一次. 当小程序后台运行或跳 ...
- uncompressing linux .................................................后没反应解决办法
编译kernel是的no machine record defined 错误,网上有一些解法,其实都是错误的,以讹传讹.不打算自己写,找到一篇还算靠谱的,转摘一下. 其根本原因是没有在 __proc_ ...
- Python9-IO模型-day41
# 进程:启动多个进程,进程之间是由操作系统负责调用# 线程:启动多个线程,真正由被cpu执行的最小单位实际是线程# 开启一个线程,创建一个线程,寄存器.堆栈# 关闭一个线程# 协程# 本质上是一个线 ...
- django开发基础
一.配置静态文件 https://www.cnblogs.com/lshedward/p/10351051.html 二.路由分发 https://www.cnblogs.com/lshedward/ ...