**题目链接:**https://vjudge.net/problem/Gym-101964E
**题意:**有n只鱼,m个钓鱼的人,每条鱼有一个x和y坐标,每个人只有一个x坐标,y坐标等于0,当 |鱼.x-人.x|+鱼.y<=l,这个人就可以钓到这条鱼,问你每个人最多能钓到几条鱼。
**分析:**按x坐标对每个人从小到大排序,然后遍历每条鱼,每条鱼都有一个能被钓到的上下界x坐标,lower_bound查找下界x坐标在人数组的位置,upper_bound查找上界x坐标在人数组的位置。详情见代码。
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int maxn =200000+100;
struct fish
{
int x,y;
}a[maxn];
struct fisher
{
int x,id;
bool operator < (const fisher& rhs)const {
return x < rhs.x;
}
}b[maxn];
int main()
{
int n,m,l;
int ans[maxn],sum[maxn];
while(scanf("%d %d %d",&n,&m,&l)!=EOF)
{
memset(sum,0,sizeof(sum));
for(int i=0; i<n; i++)
scanf("%d %d",&a[i].x,&a[i].y);
for(int i=1; i<=m; i++)
{
scanf("%d",&b[i].x);
b[i].id=i;
}
sort(b+1,b+m+1);
for(int i=0; i<n; i++)
{
if(a[i].y>l)
continue;
int left,right;
fisher tmp;
tmp.x=a[i].x+a[i].y-l;
left=lower_bound(b+1,b+m+1,tmp)-b;
tmp.x=a[i].x-a[i].y+l;
right=upper_bound(b+1,b+m+1,tmp)-b;
sum[left]++;
sum[right]--;
}
for(int i=1; i<=m; i++)
{
sum[i]+=sum[i-1];
ans[b[i].id]=sum[i];
}
for(int i=1; i<=m; i++)
printf("%d\n",ans[i]);
}
return 0;
}