zoukankan      html  css  js  c++  java
  • 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.

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

    Hint:题意就是每个人有一个money和friendship factor,选取一些人出来,这些人保证要两两之间不超过的money不超过给定值,怎么才能使得这些人的factor最大。

    思路:按照money升序排列,用尺取法(好像学了之后第一次用这个方法啊)... 当时记得引入这个尺取法的时候,是用了一个找出不超过某个数的最小区间。 这个方法的思想就是两个指针(好像也叫two-points)l 和 r 不断移动来找最优解。。。我记得网上有个图对这个方法描述的很形象。。

    。。。对就是这个,我觉得很形象啊(主要是妹子好看,逃...)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n, d;
     4 pair<int, int>a[100005];
     5 long long sum=0;
     6 
     7 int main(){
     8     scanf("%d %d", &n, &d);
     9     for(int i=1; i<=n; i++){
    10         scanf("%d %d", &a[i].first, &a[i].second);
    11     }
    12     sort(a+1, a+1+n);
    13     long long l = 1, r = 1, Max = 0;
    14     for( ; l <= n; ){
    15         if((a[r].first - a[l].first) < d){
    16             while((a[r].first - a[l].first) < d && r<=n){
    17                 sum += a[r].second;
    18                 r++;
    19                 //cout<<sum[l]<<endl;
    20             }
    21         }
    22         Max=max(Max, sum);
    23         if(r >= n+1) break;
    24         sum-=a[l].second;
    25         l++;
    26     }
    27     cout<<Max<<endl;
    28     return 0;
    29 }
  • 相关阅读:
    java字符串实现正序和倒序输出
    暑假前挑战赛1—— A,B题解
    深搜
    poj 1200 Crazy Search
    poj 1840 Eqs (hash)
    Choose the best route
    一个人的旅行
    畅通工程续
    最短路基础算法
    完全背包问题
  • 原文地址:https://www.cnblogs.com/ledoc/p/6707773.html
Copyright © 2011-2022 走看看