zoukankan      html  css  js  c++  java
  • codeforces 580B Kefa and Company

    B. Kefa and Company

     
     

    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 mi, si (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.

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 typedef long long ll;
     6 struct interval
     7 {
     8     int v,num;
     9 }p[100005];
    10 bool cmp(interval a,interval b)
    11 {
    12     return a.v<b.v;
    13 }
    14 int main()
    15 {
    16     int n,d;
    17     scanf("%d%d",&n,&d);
    18     for(int i=0;i<n;i++)
    19         scanf("%d%d",&p[i].v,&p[i].num);
    20     sort(p,p+n,cmp);
    21     ll ans=0,cnt=0;
    22     int l=0,r=0;
    23     while(r<n)
    24     {
    25         if(p[r].v-p[l].v<d)cnt+=p[r].num,r++;
    26         else cnt-=p[l].num,l++;
    27         ans=max(cnt,ans);
    28     }
    29     printf("%I64d
    ",ans);
    30     return 0;
    31 }
  • 相关阅读:
    yarn 国内加速,修改镜像源
    Gradle Wrapper 介绍
    使用 Gradle 快速创建 Java 项目
    Gradle 安装
    gradle 国内加速,修改镜像源
    maven 国内加速,修改镜像源
    java如何对map进行排序详解(map集合的使用)
    Java8 Collections.sort()及Arrays.sort()中Lambda表达式及增强版Comparator的使用
    给定字符串正整数,进行排序并返回排序后字符串
    Linux学习118 nginx与php结合实现web网站及优化
  • 原文地址:https://www.cnblogs.com/homura/p/4988050.html
Copyright © 2011-2022 走看看