博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 580B Kefa and Company
阅读量:6143 次
发布时间:2019-06-21

本文共 2714 字,大约阅读时间需要 9 分钟。

Description:

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!

Input:

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 misi (0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively.

Output:

Print the maximum total friendship factir that can be reached.

Sample Input:

Input:
4 5 75 5 0 100 150 20 75 1
Output:
100
Input:
5 100 0 7 11 32 99 10 46 8 87 54
Output:
111

Hint:

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个朋友,现在他需要朋友陪同去吃饭,但是陪同的朋友是有条件的(如果有人比其中一人的钱多至少d元,这个人就会不开心),现在他希望陪同的朋友都能开心,并且陪同的朋友友谊值最高。

#include
#include
#include
#include
#include
#include
using namespace std;const int N=1e5+10;const int INF=0x3f3f3f3f;struct node{ long long num, fee; ///存放钱的数量和友谊值}no[N];long long s[N]; ///存放第1个人到第i个人之间的友谊和int cmp(node s1, node s2){ if (s1.num != s2.num) return s1.num < s2.num; return s2.fee < s1.fee;}int main (){ long long n, i, j, idex; long long ans, sum, d; while (scanf("%lld %lld", &n, &d) != EOF) { for (i = 0; i < n; i++) scanf("%lld %lld", &no[i].num, &no[i].fee); sort(no, no+n, cmp); s[0] = no[0].fee; for (i = 1; i < n; i++) s[i] = s[i-1] + no[i].fee; ans = -INF; i = 0; j = n-1; while (i < n) ///二分查找每一个符合条件的范围 { long long l, r, m; idex = i; ///存放从i开始的最右边的满足条件的下标 l = i; r = j; while (l <= r) { m = (l+r)/2; if (no[m].num-no[i].num < d) ///说明m左边的都满足条件,继续查找右边 { l = m+1; idex = m; } else r = m-1; } sum = s[idex]-s[i]+no[i].fee; ///计算满足条件的友谊和 ans = max(ans, sum); i++; } printf("%lld\n", ans); } return 0;}

转载于:https://www.cnblogs.com/syhandll/p/4872357.html

你可能感兴趣的文章
Android Jni调用浅述
查看>>
CodeCombat森林关卡Python代码
查看>>
第一个应用程序HelloWorld
查看>>
(二)Spring Boot 起步入门(翻译自Spring Boot官方教程文档)1.5.9.RELEASE
查看>>
Java并发编程73道面试题及答案
查看>>
企业级负载平衡简介(转)
查看>>
ICCV2017 论文浏览记录
查看>>
科技巨头的交通争夺战
查看>>
当中兴安卓手机遇上农行音频通用K宝 -- 卡在“正在通讯”,一直加载中
查看>>
Shell基础之-正则表达式
查看>>
JavaScript异步之Generator、async、await
查看>>
讲讲吸顶效果与react-sticky
查看>>
c++面向对象的一些问题1 0
查看>>
直播视频流技术名词
查看>>
IOC —— AOP
查看>>
比特币现金将出新招,推动比特币现金使用
查看>>
数据库的这些性能优化,你做了吗?
查看>>
某大型网站迁移总结(完结)
查看>>
部署SSL证书后,网页内容造成页面错误提示的处理办法
查看>>
MS SQLSERVER通用存储过程分页
查看>>