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. 使用JAVA直观感受快速排序与冒泡排序的性能差异

    初学算法,肯定会编写排序算法 其中两个最为有名的就是冒泡排序和快速排序 理论上冒泡排序的时间复杂度为O(N^2),快速排序的时间复杂度为O(NlogN) 下面本门使用JAVA,分别编写三段排序程序 对 ...

  2. R语言,NA,NAN

    好莫名其妙的结果 is.na() #NA得不到的值is.nan() #NAN不可能的值is.infinite() #无穷的 x1<-NA x2<-0/0x3<-1/0 is.na(x ...

  3. 07_js走路小游戏

    <html> <head> <!-- 不做了,思路: 按enter键停止,将xs,ys替换为0,再次按,判断xs和ys是否为0,是的话,讲根据fx给xsys赋值. 实现鼠 ...

  4. java中对浮点数精度的处理DecimalFormat

    DecimalFormat是一个队浮点数进行格式化输出的利器,比如我们要输出一个保留一位小数的浮点数,可以键入如下代码: DecimalFormat df = new DecimalFormat(&q ...

  5. 不要随随便便的distinct和order by

    相关查询非常慢,通过程序拿到了相关sqlexplainexplain SELECT DISTINCT(o.orders_id), o.oa_order_id, customers_email_addr ...

  6. 安卓 Dialogs(对话框)

    转载自:http://www.apkbus.com/home.php?mod=space&uid=679028&do=blog&id=61197 对话框是一个小的窗口用以提示用 ...

  7. WebView增加一个水平Progress,位置、长相随意

    实际效果可以参看微信的web页面进度条 本质就是通过addView及对WebView 页面进度进行监听 先看看这个自定义的DrawableId,我们参照系统默认实现的方法写一个自己的   <la ...

  8. python 对数函数

    from math import logfrom math import e print e #自然对数print log(e) #log函数默认是以e为底print log(100,10) #以10 ...

  9. OpenLayers调用ArcGIS Server发布的WFS服务

    OpenLayers调用ArcGIS Server发布的WFS服务 原创: 蔡建良 2013-08-20 一. 开发环境 1) Openlayers2.13+arcgis server9.3 2) W ...

  10. c排序算法大全

    排序算法是一种基本并且常用的算法.由于实际工作中处理的数量巨大,所以排序算法 对算法本身的速度要求很高. 而一般我们所谓的算法的性能主要是指算法的复杂度,一般用O方法来表示.在后面将给出详细的说明.& ...