cf 580B
来源:互联网 发布:康亿点验钞机升级软件 编辑:程序博客网 时间:2024/05/21 15:07
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!
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.
Print the maximum total friendship factir that can be reached.
4 575 50 100150 2075 1
100
5 1000 711 3299 1046 887 54
111
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内的值
#include <iostream>#include <stdio.h>#include <string.h>#include <cmath>#include <algorithm>#include <map>#include <queue>using namespace std;const int maxn=100000+10;const int inf=(1<<30);struct Node{ __int64 num,val;}p[maxn];bool cmp(Node a,Node b){ return a.num<b.num;}__int64 ans[maxn];int main(){ int n,d; while(~scanf("%d%d",&n,&d)) { for(int i=0;i<n;i++) { scanf("%I64d%I64d",&p[i].num,&p[i].val); } memset(ans,0,sizeof(ans)); sort(p,p+n,cmp); __int64 cnt=0; for(int i=0;i<n;i++) { if(abs(p[i].num-p[cnt].num)<d) ans[cnt]+=p[i].val; else { cnt++; ans[cnt]=ans[cnt-1]-p[cnt-1].val; i--; } } sort(ans,ans+cnt+1); printf("%I64d\n",ans[cnt]); } return 0;}
- cf 580B
- cf-B
- cf B
- CF 1B Spreadsheet
- CF 3B Lorry
- CF 176B
- CF 91B
- cf 131 DIV2 B
- CF 113B || CF196D
- CF 126B
- CF-236B
- CF 254B(日期)
- cf 79B
- CF - 158B - Taxi
- CF 296B
- CF 173(div2) B
- cf:B-QR code
- cf 1B. Spreadsheets
- main函数的两个参数浅析
- IE版本的语句和placeholder——疑问1
- cf 580 A
- HDU 1054 Strategic Game(简单树形DP)
- 修改时区(RHEL6.5)
- cf 580B
- hdoj 4196 Remoteland 【阶乘因式分解中质因子个数 + 费马小定理】
- java文件切割工具
- 软件重构
- GVim for windows 简单使用教程
- 重载和多态之间的关系?!(C++初学者)
- no lable views point to this text field with an android:lablFor=”@+id/@+id
- java注解
- 将 Sublime 3 打造成 Python/Django IDE开发利器