接上一篇 580 B. Kefa and Company [ Codeforces Round #321 (Div. 2)]

简介:
B. Kefa and Company
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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!

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 test(s)
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
Note

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个朋友,他想找他的n个朋友来玩,但是朋友间会相互攀比,就不能带他们的财富差距大于或等于d的,
他的朋友间还有好友度
求来的最大的朋友的友好度good
首先给你两个数n和d,分别代表有n个朋友,然后不超过d,
下面有n行,分别表示朋友的金钱数目和友好度,

解题思路:
先按照钱的多少排一下序,然后暴力寻找最大的好友度

具体详见代码:
/**
2015 - 09 - 24 晚上

Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e5+5;
const int mod = 1e9+7;
const double eps = 1e-7;
struct node
{
    int good, money;
}arr[maxn];

bool cmp(node a, node b)
{
    return a.money < b.money;
}

int main()
{
    int n, d;
    LL sum=0, ret=0;
    cin>>n>>d;
    for(int i=0; i<n; i++)
        cin>>arr[i].money>>arr[i].good;
    sort(arr, arr+n, cmp);
    int st=0, end=0;
    while(end < n)
    {
        while(arr[end].money-arr[st].money<d && end<n)
        {
            sum += arr[end].good;
            end++;
        }
        ret = max(ret, sum);
        sum -= arr[st].good;
        ///sum = 0;
        st++;
    }
    cout<<ret<<endl;
    return 0;
}


目录
相关文章
|
8月前
|
机器学习/深度学习 人工智能 移动开发
.Codeforces Round 883 (Div. 3)
Codeforces Round 883 (Div. 3)
|
11月前
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) D2
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) D2
56 0
|
11月前
Codeforces Round 799 (Div. 4)
Codeforces Round 799 (Div. 4)
91 0
|
11月前
Codeforces Round 640 (Div. 4)
Codeforces Round 640 (Div. 4)A~G
64 0
Codeforces Round #644 (Div. 3)(A~G)
Codeforces Round #644 (Div. 3)(A~G)
94 0
|
机器学习/深度学习
|
机器学习/深度学习 算法 C++
|
人工智能 算法
|
人工智能 算法