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无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多 ...
 
随机推荐
- python 线程的调用方式
			
python 线程的调用方式 #!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/5/24 9:44 # ...
 - PAT (Advanced Level) Practise -  1095. Cars on Campus (30)
			
http://www.patest.cn/contests/pat-a-practise/1095 Zhejiang University has 6 campuses and a lot of ga ...
 - AJAXA进行分页(2)
			
查询功能是开发中最重要的一个功能,大量数据的显示,我们用的最多的就是分页. 在ASP.NET 中有很多数据展现的控件,比如用的最多的GridView,它同时也自带了分页的功能.但是我们知道用GridV ...
 - Oracle 配置文件目录
			
Oracle 配置文件目录 ① 在oracle安装目录下,找D:\oracle\product\10.2.0\client_1\NETWORK\ADMIN中的tnsnames.ora文件,找到之后,配 ...
 - Vuex的简单了解
			
vuex的官网了解:https://vuex.vuejs.org/zh/guide/ 一.什么是vuex? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所 ...
 - SummerVocation_Learning--java的自动打包与解包
			
Auto Boxing: 自动将基础类型转换成对象(JDK1.5之后支持) Auto UnBoxing:自动将对象转换成基础类型 如 Map中的put方法,如果要传入键值对<a,1>,&l ...
 - 【思维题  最大权闭合子图】loj#6045. 「雅礼集训 2017 Day8」价
			
又是经典模型的好题目 题目描述 人类智慧之神 zhangzj 最近有点胖,所以要减肥,他买了 NN 种减肥药,发现每种减肥药使用了若干种药材,总共正好有 NN 种不同的药材. 经过他的人脑实验,他发现 ...
 - 十二、MySQL 查询数据
			
MySQL 查询数据 MySQL 数据库使用SQL SELECT语句来查询数据. 你可以通过 mysql> 命令提示窗口中在数据库中查询数据,或者通过PHP脚本来查询数据. 语法 以下为在MyS ...
 - 4-8 string
			
1.常用的string模块 import string # 26个小写字母 print(string.ascii_lowercase) # abcdefghijklmnopqrstuvwxyz # 2 ...
 - Python学习笔记(二):数据类型
			
一.python中的数据类型 python中的数据类型包括:整型.浮点型.布尔型.字符串类型 整型(int)和浮点型(float) Python中的整型只有int,没有short.long:浮点型fl ...