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. CSS构造表单

    结构化表单布局 <head> <meta http-equiv="Content-Type" content="text/html; charset=G ...

  2. Chrome 快捷键使用

    窗口和标签页快捷方式 Ctrl+N 打开新窗口 按住 Ctrl‎ 键,然后点击链接 在新标签页中打开链接 按住 Shift 键,然后点击链接 在新窗口中打开链接 Alt+F4 关闭当前窗口 Ctrl+ ...

  3. 指针数组vs数组指针 指针函数vs函数指针

    在分辨这些重要的概念时,我们先回顾一下前面所讲的C之三值合一,由于三个值所求出的地址是相同的,所以经常有传言说他们都是首元素的地址.这种说法是不正确的.为什么说它是不正确的呢? 首先定义一个指针,将三 ...

  4. (转)FirstResponder 释放问题

    FirstResponder 释放问题 转自:http://www.cnblogs.com/smileEvday/archive/2012/03/18/2405190.html View的FirstR ...

  5. 8个必备的PHP功能开发 (转)

    做过PHP开发的程序员应该清楚,PHP中有很多内置的功能,掌握了它们,可以帮助你在做PHP开发时更加得心应手,本文将分享8个开发必备的PHP功能,个个都非常实用,希望各位PHP开发者能够掌握. 1.传 ...

  6. 08day1

    高中运动会 最大公约数 [问题描述] 梦幻城市每年为全市高中生兴办一次运动会.为促使各校同学之间的交流,采用特别的分队方式:每一个学校的同学,必须被均匀分散到各队,使得每一队中该校的人数皆相同.为增加 ...

  7. Linux服务器偶尔无法访问问题

    最近上了一台web服务器(本地包含mysql服务器),在运行一段时间发现服务器偶尔会无法访问, 包括mysql,ftp以及ssh等都无法响应,但是已经连接上的ssh不受任何影响,在查看系统log时, ...

  8. rsync同步时报“auth failed on module”错误的可能原因

    关于这个auth失败的问题,有以下可能的情况:   1.密码输入错误: 请再次确认你登录用户的密码无误   2.secrets file格式错误: secrets file的文件格式是  upload ...

  9. nginx upstream模块

    upstream模块 upstream模块 (100%) nginx模块一般被分成三大类:handler.filter和upstream.前面的章节中,读者已经了解了handler.filter. 利 ...

  10. 已经被cocos2dx给折腾的想要放弃它,专注Unity3D的怀抱了!

    一直使用cocos2dx编写自己的2D小游戏,不得不说,编写个人的超级小规模的游戏,使用cocos2dx有一定的优势,首先门槛很低,编写2D游戏用起来也算顺手,可惜一直没有一个优秀的UI编辑器,好不容 ...