题目链接: 传送门

Kefa and Company

time limit per test:2 second     memory limit per test:256 megabytes

Description

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 Input

4 5
75 5
0 100
150 20
75 1

5 100
0 7
11 32
99 10
46 8
87 54

Sample Output

100

111

解体思路:

今天想起很多题目很补,开始补题,当时这道题搞了很久,一直超时,不懂把factor值存起来= =果然多天的训练,有一定的效果

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
struct node{
    LL money,fac;
};
node p[100005];
LL sum[100005];
bool cmp(node x,node y)
{
    if (x.money == y.money)
        return x.fac > y.fac;
    else
        return x.money < y.money;
}

int main()
{
    int n,d;
    while (~scanf("%d%d",&n,&d))
    {
        LL res = 0;
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        for (int i = 0;i < n;i++)
        {
            scanf("%I64d%I64d",&p[i].money,&p[i].fac);
        }
        sort(p,p+n,cmp);
        for (int i = 0;i < n;i++)
        {
            sum[i+1] = sum[i] + p[i].fac;
        }
        int i= 0,j = 1;
        while (i < n)
        {
            while (j < n && p[j].money -p[i].money < d)
            {
                j++;
            }
            res = max(res,sum[j] - sum[i]);
            i++;
        }
        printf("%I64d\n",res);
    }
    return 0;
}

CF 321B Kefa and Company(贪心)的更多相关文章

  1. B - Kefa and Company

    B - Kefa and Company Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  2. Codeforces Round #321 (Div. 2) B. Kefa and Company 二分

    B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...

  3. Codeforces Round #321 (Div. 2)-B. Kefa and Company,区间最大值!

    ->链接在此<- B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes ...

  4. CF #374 (Div. 2) D. 贪心,优先队列或set

    1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...

  5. CF580B Kefa and Company 尺取法

    Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa ...

  6. CF 435B Pasha Maximizes(贪心)

    题目链接: [传送门][1] Pasha Maximizes time limit per test:1 second     memory limit per test:256 megabytes ...

  7. [CF #288-C] Anya and Ghosts (贪心)

    题目链接:http://codeforces.com/contest/508/problem/C 题目大意:给你三个数,m,t,r,代表晚上有m个幽灵,我有无限支蜡烛,每支蜡烛能够亮t秒,房间需要r支 ...

  8. cf 605A Sorting Railway Cars 贪心 简单题

    其实就是求总长度 - 一个最长“连续”自序列的长度 最长“连续”自序列即一个最长的lis,并且这个lis的值刚好是连续的,比如4,5,6... 遍历一遍,贪心就是了 遍历到第i个时,此时值为a[i], ...

  9. Codeforces 580B: Kefa and Company(前缀和)

    http://codeforces.com/problemset/problem/580/B 题意:Kefa有n个朋友,要和这n个朋友中的一些出去,这些朋友有一些钱,并且和Kefa有一定的友谊值,要求 ...

随机推荐

  1. Ext.NET-布局篇

    概述 前一篇介绍了Ext.NET基础知识,并对Ext.NET布局进行了简要的说明,本文中我们用一个完整的示例代码来看看Ext.NET的布局. 示例代码下载地址>>>>> ...

  2. HTTP请求头参数

      Accept-Language: zh-cn,zh;q=0.5 意思:浏览器支持的语言分别是中文和简体中文,优先支持简体中文. 详解: Accept-Language表示浏览器所支持的语言类型: ...

  3. 数据源DBCP一二

    其实DBCP这个数据源实际上和com.alibaba.druid.pool.DruidDataSource 是差不多的

  4. html+js实现图片预览

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Beta版本冲刺———第二天

    会议照片: 项目燃尽图: 1.项目进展: 昨天的困难:分数排行榜的设计 今天解决的进度:完成了界面优化以及建立新的排行榜选项卡界面. 明天要做的事情:分数排行榜的功能设计 2.每个人每天做的事情 郭怡 ...

  6. 1031MVCC和事务浅析

    转自 http://blog.csdn.net/sofia1217/article/details/50778906 关于MVCC浅析,有些难度http://xuebinbin212.blog.163 ...

  7. 网站移植到linux上后常犯的错误

    常犯的错误 1:gcc库没装或者没装全 表现:没有可用的C编译器 同类错误:提示g++ not found, 解决:出现以上错误,则是因为gcc编译器没装,或者是没装全. 挂载光盘,到Pakeges里 ...

  8. c#模拟表单POST数据,并获取跳转之后的页面

    直接看代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...

  9. VMWare虚拟机提供的桥接、nat和主机模式的区别

    虚拟机网络模式 无论是vmware,virtual box,virtual pc等虚拟机软件,一般来说,虚拟机有三种网络模式: 1.桥接 2.NAT 3.Host-Only 哪一种网络是适合自己的虚拟 ...

  10. Notes on 'Efficient Graph-Based Image Segmentation'

    Notes on Efficient Graph-Based Image Segmentation 算法的目标 按照一种确定的标准, 将图片分割成细粒度的语义区域, 即Super pixel. 算法步 ...