codeforces 580B Kefa and Company
来源:互联网 发布:手机相机软件排行 编辑:程序博客网 时间:2024/04/29 05:00
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.
题目大意:每个朋友有自己的m和s,要求每个人之间的m值不超过d值,求最大的s总和
扫一遍就可以了
#include<iostream>#include<algorithm>#include<cstdio>using namespace std;typedef long long ll;const int maxn=1e5+7;struct Node{int m;int s;}fri[maxn];bool cmp(Node a,Node b){return a.m<b.m;}int main(){int n,d;while(cin>>n>>d){for(int i=0;i<n;i++){scanf("%d%d",&fri[i].m,&fri[i].s);}sort(fri,fri+n,cmp);int l=0,r=0;ll maxx=0;ll ans=0;while(r!=n){if(fri[r].m-fri[l].m<d){ans+=fri[r].s;maxx=max(ans,maxx);r++;}else{ans-=fri[l].s;l++;}}cout<<maxx<<endl;}}
- codeforces-580B Kefa and Company(二分)
- CodeForces 580B Kefa and Company
- Codeforces 580B Kefa and Company
- 【CodeForces】[580B]Kefa and Company
- CodeForces 580B Kefa and Company
- CodeForces 580B Kefa and Company
- 【CodeForces 580B】Kefa and Company
- codeforces 580B Kefa and Company
- Codeforces Round #321 (Div. 2) 580B. Kefa and Company
- CodeForces 580B Kefa and Company <贪心>
- 【CodeForces】580B - Kefa and Company(二分)
- Codeforces 580B: Kefa and Company(尺取法)
- Codeforces Round #321 B Kefa and Company
- B. Kefa and Company
- coderforce 580B Kefa and Company
- Codeforces Round #321 (Div. 2)B. Kefa and Company
- Codeforces Round #321 (Div. 2) B. Kefa and Company
- Codeforces Round #321 (Div. 2)B. Kefa and Company
- 02:同行列对角线的格子
- adaboost原理和example
- 查询端口被占用
- Python3.5——内置方法参数详解
- 访问页面时后台报错: 对实体 "XX" 的引用必须以 ';' 分隔符结尾
- codeforces 580B Kefa and Company
- VISIO2013秒激活
- 语谱图的生成
- webService第一天
- python_lintcode_64合并排序数组 II_60搜索插入位置_142 O(1)时间检测2的幂次
- 多线程的几种同步方式
- RGB-D SLAM——回环检测篇(四)
- Hadoop总结—思维导图
- SVN服务器搭建和使用(二)