codeforces-580B

来源:互联网 发布:淘宝新手卖家如何推广 编辑:程序博客网 时间:2024/06/07 19:28

题目:
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
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
Hint
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.

题意:
在差值为<=d的情况下求最大友谊因素

思路:
按钱进行升序排列,对排序后的每个人,二分查找能和他在一起的钱最多的人(排序后)的位置(最右边),之后便可以求能和他在一起的人的友谊因素总和(包括他自己),比较出大小。

小技巧:
由于每次得到满足条件内最多钱的人的位置后都需要求友谊因素总和,当n很大的时候会超时,技巧之一是先把每个前n个人的友谊因素综合用一个数组存储。
b[0] = a[0];
b[ i ] =a[i] + b[i-1];

只需考虑每个人之后钱之差<=d 范围内的人的友谊因素总和,之前就不需要考虑了。对序号为n的人,考虑n之前钱之差为<=d范围内的人(假设范围是n-m 到n)的友谊因素总和相当于序号为(n-m)的人考虑之后范围为(n-m 到n)的友谊因素总和。
因此这里还能优化代码的运行时间,因为low 变大。

下面是代码:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;long long a[100100];struct freindship{int money;int ship;} node[100100];bool cmp(freindship a, freindship b){    return a.money < b.money;}int main(){    int n, d;    long long sum,temp = -1;    while(scanf("%d%d",&n,&d)!=EOF)    {        for(int i=0; i<n; i++) scanf("%d%d",&node[i].money, &node[i].ship);        sort(node, node+n, cmp);        //for(int i=0; i<n; i++) printf("%d %d\n",node[i].money, node[i].ship);        a[0] = node[0].ship;        for(int i=1; i<n; i++)        {            a[i] = node[i].ship + a[i-1];        }        for(int i=0; i<n; i++)        {            int low = i;            int high = n-1;            sum = 0;            while(low <= high)            {                int mid = (low+high) / 2;                if( (node[mid].money - node[i].money) < d )                    low = mid + 1;                else high = mid - 1;            }            if(high==i)            {                sum = node[i].ship;            }            else            {                sum = a[high] - a[i-1];            }            if(sum > temp) temp = sum;        }        printf("%lld\n",temp);    }    return 0;}

个人小错误:
定义了temp 为long long 输出时却写成%d

0 0
原创粉丝点击