接上一篇 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;
}


目录
相关文章
|
5月前
Codeforces Round #729 (Div. 2)
【6月更文挑战第4天】在这两个编程问题中,B. Plus and Multiply 要求判断通过加法和乘法操作数组是否能形成目标数 `n`。思路是形如 `x^a + yb = n` 的表达式,如果满足则能构造。C. Strange Function 关注的是找到最小正整数 `x` 使得 `x` 不是 `i` 的因子,计算这些值的和模 `10^9+7`。对于每个 `i`,偶数时 `f(i)` 是 3,奇数时是 2,利用因子与最大公约数计算周期性求和。
32 1
Codeforces Round #192 (Div. 2) (329A)C.Purification
Codeforces Round #192 (Div. 2) (329A)C.Purification
45 0
|
人工智能 算法 BI
Codeforces Round #179 (Div. 2)A、B、C、D
我们每次加进来的点相当于k,首先需要进行一个双重循环找到k点和所有点之间的最短路径;然后就以k点位判断节点更新之前的k-1个点,时间复杂度降到O(n^3),而暴力解法每次都要进行floyd,时间复杂度为O(n^4);相比之下前述解法考虑到了floyd算法的性质,更好了运用了算法的内质。
54 0
Codeforces Round #192 (Div. 2) (330A) A. Cakeminator
如果某一行没有草莓,就可以吃掉这一行,某一列没有也可以吃点这一列,求最多会被吃掉多少块蛋糕。
48 0
Codeforces Round 849 (Div. 4)
Codeforces Round 849 (Div. 4)A~G2题解
116 0
Codeforces Round #723 (Div. 2)B. I Hate 1111
Description You are given an integer x. Can you make x by summing up some number of 11,111,1111,11111,…? (You can use any number among them any number of times). For instance, 33=11+11+11 144=111+11+11+11
178 0
Codeforces Round #723 (Div. 2)B. I Hate 1111
Equidistant Vertices-树型dp-Codeforces Round #734 (Div. 3)
Description A tree is an undirected connected graph without cycles. You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words,
139 0
|
机器学习/深度学习 算法 C++