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 }
  • 相关阅读:
    让外部网络访问K8S service的四种方式
    Kubernetes 针对资源紧缺处理方式的配置
    基于Prometheus,Alermanager实现Kubernetes自动伸缩
    ubuntu16.04下python2、python3环境选择与python升级(pip版本切换)
    学习资源集锦
    tensorflow :ckpt模型转换为pytorch : hdf5模型
    SoftMax多分类器原理及代码理解
    玩转树莓派3
    keras_基本网络层结构(2)_卷积层
    keras_基本网络层结构(1)_常用层
  • 原文地址:https://www.cnblogs.com/homura/p/4988050.html
Copyright © 2011-2022 走看看