题目大意:Kefa要请朋友吃饭,他有n个朋友,这些朋友都两个特征:1.身上所带钱数2.对Kefa的友谊值
如果这些朋友中有人所带钱数比这个朋友所带钱所多与超过d元(包括d),那么这朋友会觉得自己可怜,Kefa
不想让自己的朋友感到可怜,但他又想获得高得友谊值,问Kefa能获得的最高的友谊值是多少
解题:
将朋友所带的钱数和友谊值排序,按钱数从小到大排然后进行判断处理
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
const LL maxm=1e5+10;
struct node
{
LL m,s;
bool operator<(const node &a)const
{
return m<a.m;
}
}t[maxm];
int main()
{
LL m,d;
while(scanf("%lld%lld",&m,&d)!=EOF)
{
for(LL i=0;i<m;i++)
{
scanf("%lld%lld",&t[i].m,&t[i].s);
}
sort(t,t+m);
LL sum=0,l=0,r=0,M=0;
while(l<m)
{
if(t[l].m-t[r].m>=d)
{
sum-=t[r].s;
r++;
}
else
{
sum+=t[l].s;
l++;
}
M=max(M,sum);
}
printf("%lld\n",M);
}
return 0;
}