zoukankan      html  css  js  c++  java
  • CF div2 321 B

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

    读题不清楚真是大问题,然后想二分可以搞,于是读错题的情况下调bug调的想死,然后放弃治疗,换一种方法。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <stack>
     5 #include <queue>
     6 #include <map>
     7 #include <algorithm>
     8 #include <vector>
     9 #include <cmath>
    10 
    11 using namespace std;
    12 
    13 const int maxn = 1000005;
    14 
    15 typedef long long LL;
    16 
    17 vector<int>G[maxn];
    18 
    19 
    20 struct node
    21 {
    22     int x,y;
    23 }a[maxn];
    24 
    25 
    26 bool cmp(node a,node b)
    27 {
    28     if(a.x == b.x) return a.y<b.y;
    29    return a.x<b.x;
    30 }
    31 
    32 
    33 
    34 int main()
    35 {
    36     int n,m;
    37 
    38     while(scanf("%d%d",&n,&m)!=EOF){
    39     for(int i=1;i<=n;i++){
    40          scanf("%d%d",&a[i].x,&a[i].y);
    41     }
    42 
    43     sort(a+1,a+n+1,cmp);
    44     LL sum = 0;
    45     LL ans = -999999999999;
    46     int l = 1;
    47     for(int i=1;i<=n;i++){
    48         while(a[i].x-a[l].x>=m)
    49             sum -= a[l++].y;
    50             sum += a[i].y;
    51             ans = max(sum,ans);
    52     }
    53 
    54     printf("%lld
    ",ans);
    55 }
    56 
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    进击Node.js基础(一)
    关于bootstrap两个模态框的问题
    系列博文-Three.js入门指南(张雯莉)-网格 setInterval方法 requestAnimationFrame方法 使用stat.js记录FPS
    系列博文-Three.js入门指南(张雯莉)-照相机
    系列博文-Three.js入门指南(张雯莉)-静态demo和three.js功能概览
    for循环执行效率
    c/c++多维数组动态分配与释放
    C/C++数组指针与指针数组详解
    C/C++语言参数传递----值传递、引用传递、指针传递、指针引用传递
    float类型最大值和最小值
  • 原文地址:https://www.cnblogs.com/lmlyzxiao/p/4834058.html
Copyright © 2011-2022 走看看