B - Kefa and Company

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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.

题目大意:Kefa要请朋友吃饭,他有n个朋友,这些朋友都两个特征:1.身上所带钱数2.对Kefa的友谊值

如果这些朋友中有人所带钱数比这个朋友所带钱所多与超过d元(包括d),那么这朋友会觉得自己可怜,Kefa

不想让自己的朋友感到可怜,但他又想获得高得友谊值,问Kefa能获得的最高的友谊值是多少

解题:

将朋友所带的钱数和友谊值排序,按钱数从小到大排然后进行判断处理

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std; const int N = ;
typedef long long ll; struct st
{
ll m, s;
}node[N]; int cmp(const void *a, const void *b)
{
st *s1 = (st *)a, *s2 = (st *)b;
if(s1->m != s2->m)
return s1->m - s2->m;
return s1->s - s2->s;
} int main()
{
int n, i;
ll d, sum;
while(~scanf("%d%I64d", &n, &d))
{
for(i = ; i < n ; i++)
scanf("%I64d%I64d", &node[i].m, &node[i].s);
qsort(node, n, sizeof(node[]), cmp);
sum = i = ;
int j = ;
ll Max = ;
while(i < n)
{
if(node[i].m - node[j].m >= d)
{
sum -= node[j].s;
j++;
}
else
{
sum += node[i].s;
i++;
}
Max = max(Max, sum);
}
printf("%I64d\n", Max);
}
return ;
}

B - Kefa and Company的更多相关文章

  1. CF 321B Kefa and Company(贪心)

    题目链接: 传送门 Kefa and Company time limit per test:2 second     memory limit per test:256 megabytes Desc ...

  2. 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 ...

  3. Codeforces Round #321 (Div. 2)-B. Kefa and Company,区间最大值!

    ->链接在此<- B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes ...

  4. CF580B Kefa and Company 尺取法

    Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa ...

  5. Codeforces 580B: Kefa and Company(前缀和)

    http://codeforces.com/problemset/problem/580/B 题意:Kefa有n个朋友,要和这n个朋友中的一些出去,这些朋友有一些钱,并且和Kefa有一定的友谊值,要求 ...

  6. [CF580B]Kefa and Company(滑动窗口)

    题目链接:http://codeforces.com/problemset/problem/580/B 某人有n个朋友,这n个朋友有钱数m和关系s两个属性.问如何选择朋友,使得这些朋友之间s最大差距小 ...

  7. 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)

    题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...

  8. Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)

    排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...

  9. Codeforces Round #321 (Div. 2) Kefa and Company 二分

    原题链接:http://codeforces.com/contest/580/problem/B 题意: 给你一个集合,集合中的每个元素有两个属性,$m_i,s_i$,让你求个子集合,使得集合中的最大 ...

随机推荐

  1. laravel中的$request对象构造及请求生命周期

    laravel应用程序中index.php是所有请求的入口.当用户提交一个form或者访问一个网页时,首先由kernel捕捉到该session PHP运行环境下的用户数据, 生成一个request对象 ...

  2. Qt之等待提示框(QMovie)

    简述 关于gif的使用在实际项目中我用的并不多,因为我感觉瑕疵挺多的,很多时候锯齿比较严重,当然与图存在很大的关系. 关于生成gif的方法可以提供一个网站preloaders,基本是可以满足需求的. ...

  3. BZOJ 3694 最短路

    233333想简单了.... 题解参见http://hzwer.com/3710.html #include<iostream> #include<cstdio> #inclu ...

  4. 使用NPOI创建Excel文件

    Public Sub BuildExcel() '写入内容到Excel Dim hssfworkbook As HSSFWorkbook = WriteExcel() Dim destFileName ...

  5. dede 5.7进后台卡死解决办法

    注释后台文件dede/templets/index_body.htm(大概在第18行) <script type="text/javascript"> function ...

  6. 【英语】Bingo口语笔记(45) - Pass系列

  7. 多数据源问题--Spring+Ibatis 访问多个数据源(非分布式事务)

    有的时候,我在一个工程中需要访问两个以上的数据源,尤其是在系统集成的时候,以下是我在系统集成的时候遇到的情况,我的工程的架构是:spring2.0+ibatis2.0+struts1.2. 数据库是o ...

  8. WINHEX 使用教程

    Winhex有完善的分区管理功能和文件管理功能,能自动分析分区链和文件簇链,能对硬盘进行不同方式不同程度的备份,甚至克隆整个硬盘:它能够编 辑任何一种文件类型的二进制内容(用十六进制显示)其磁盘编辑器 ...

  9. table应用之colspan与rowspan

    <table border=" borderColorDark="#66ff33"> <tr> <td rowspan=" ali ...

  10. nginx爆出新漏洞 最低限度可造成DDos攻击

    5月9日消息:国内某安全厂商称HTTP代理服务器nginx爆出远程栈缓冲区溢出漏洞,攻击者利用此漏洞可能造成栈溢出,从而执行任意代码,最低限度可造成拒绝服务攻击.目前,官方已经发布安全公告以及相应补丁 ...