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无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多 ...
随机推荐
- CSS 样式、布局、盒子模型
Css内容: 常用样式: 字体 颜色 背景 布局: 浮动 定位 标签特性 标签盒子模型: 边距 边框 动画: 旋转 渐变 注意:Css引路径从css文件里找 Html和js ...
- java面试题:如果一串字符如"aaaabbc中国1512"要分别统计英文字符的数量,中文字符的数量,和数字字符的数量,假设字符中没有中文字符、英文字符、数字字符之外的其他特殊字符。
package com.swift; public class TotalNumber_String { public static void main(String[] args) { /* * 如 ...
- [NOI2007]货币兑换Cash
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 6353 Solved: 2563[Submit][Status][Discuss] Descriptio ...
- 删除Zend Studio项目
导入了过大的项目,导致很卡,且Close Project和Delete操作不了,一直无响应. 调整项目目录下的隐藏文件夹,删除对应项目: E:\www\.metadata\.plugins\org.e ...
- django开发傻瓜教程-1-安装和HelloWorld
安装 sudo pip install Django 新建项目 django-admin startproject XXX 启动项目 进入主目录下 python manage.py runserver ...
- 动态规划:POJ2576-Tug of War(二维费用的背包问题)
Tug of War Time Limit: 3000MS Memory Limit: 65536K Description A tug of war is to be arranged at the ...
- 大数运算:HDU-1042-N!(附N!位数的计算)
解题心得: 这里使用了10000进制.很明显,因为是n!所以单个最大的数是10000*10000,使用万进制. 可以借鉴高精度的加法,单个乘了之后在进位. 很坑的一点,0!=1,数学不好WA了三次,尴 ...
- Android开发——常见的内存泄漏以及解决方案(二)
)Android2.3以后,SoftReference不再可靠.垃圾回收期更容易回收它,不再是内存不足时才回收软引用.那么缓存机制便失去了意义.Google官方建议使用LruCache作为缓存的集合类 ...
- cacheData
<%@ page language="java" import="java.util.*,com.fiberhome.bcs.appprocess.common.u ...
- Python框架之Django学习笔记(一)
Django历史: Django 是从真实世界的应用中成长起来的,它是由 堪萨斯(Kansas)州 Lawrence 城中的一个 网络开发小组编写的. 它诞生于 2003 年秋天,那时 Lawrenc ...