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 }
  • 相关阅读:
    用Iterator实现遍历集合
    SimpleDateFormat使用详解 <转>
    Java学习之Iterator(迭代器)的一般用法 (转)
    Java:String和Date、Timestamp之间的转换
    关于PreparedStatement.addBatch()方法 (转)
    JavaBean入门及简单的例子
    Tomcat7.0无法启动解决方法[failed to start]
    executeQuery、executeUpdate 和 execute
    jquery中attr和prop的区别
    Jquery的parent和parents(找到某一特定的祖先元素)
  • 原文地址:https://www.cnblogs.com/homura/p/4988050.html
Copyright © 2011-2022 走看看