B. Kefa and Company
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/580/problem/BDescription
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
Sample Output
100
HINT
题意
给你n个人,告诉你每个人拥有多少钱,并且好感度是多少
如果在公司里面存在有一个人的钱大于等于他的话,这个人就会不开心
然后问你怎么安排,使得好感度最高
题解:
先排序,然后暴力
维护一个区间,这个区间都是可以选择的,至于怎么维护这个区间,可以尺取法,也可以而且搞
看自己咯
代码:
//qscqesze #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::ssecondnc_with_stdio(0);cin.tie(0) #define maxn 100006 #define mod 1000000007 #define eps 1e-9 #define PI acos(-1) const double EP = 1E-10 ; int Num; //const int inf=0first7fffffff; const ll inf=999999999; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************* ll sum[maxn]; struct node { ll first,second; friend bool operator < (const node & x,const node & y) { return x.first < y.first; } }; node A[maxn]; int main() { int n=read(); ll d=read(); for(int i=1;i<=n;i++) A[i].first = read(),A[i].second=read(); sort(A+1,A+1+n); for(int i=1;i<=n;i++) sum[i]=sum[i-1]+A[i].second; ll ans = 0; ll ddd =0; for(int i=1;i<=n;i++) { int r = upper_bound(A+1,A+1+n,node{A[i].first+d-1LL,ddd}) - A; ans = max(ans,sum[r-1]-sum[i-1]); } printf("%I64d ",ans); }