分析:题意:给你一个n个点,再给你个正方形,问你能覆盖最多的点事多少?
正方形从一个点一个点的挪过去。先对x坐标排序,从最左边的点开始,在正方形左右边允许的范围内,把该区域内的y坐标排序,正方形在该区域内向上挪,这样遍历就行了
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int a[1005];
struct A
{
int x,y;
}ch[1002];
bool cmp(A a, A b)
{
return a.x < b.x;
}
int main()
{
int n, r;
while(scanf("%d%d", &n, &r) != EOF)
{
int i, j, k;
for(i = 0; i < n; i++)
{
scanf("%d%d", &ch[i].x, &ch[i].y);
}
sort(ch, ch+n, cmp);
int sum = 0, count = 0;
for(i = 0; i < n; i++)
{
k = 0;
for(j = i; ch[j].x <= ch[i].x+r && j < n; j++)
a[k++] = ch[j].y;
sort(a, a+k);
int temp = 0, p = 0;
for(j = 0; j < k && temp < k; j++)// temp < k这里w了好久
{
while(a[temp] -a[j] <= r && temp < k)
temp++;
if(p < temp - j)
p = temp - j;
}
if(count < p)
count = p;
}
printf("%d\n", count);
}
return 0;
}