题目链接:http://codeforces.com/problemset/problem/580/B点击打开链接
排序后尺取
#include <bits/stdc++.h>
using namespace std;
struct xjy
{
int money;
long long int friendship;
bool operator < (const xjy &r)const
{
return money<r.money;
}
};
int main()
{
int n,d;
vector<xjy > s;
cin >>n >>d;
for(int i=0;i<n;i++)
{
xjy mid;
cin >> mid.money >> mid.friendship;
s.push_back(mid);
}
sort(s.begin(),s.end());
int l=0,r=0;
long long int sum=s[0].friendship;
long long int ans=s[0].friendship;
while(r!=n-1)
{
r++;
sum+=s[r].friendship;
while(s[r].money-s[l].money>=d)
{
sum-=s[l].friendship;
l++;
}
ans=max(ans,sum);
}
cout << ans << endl;
}