题目:http://acm.hdu.edu.cn/showproblem.php?pid=4007
给定一些点和一个正方形,求正方形能圈住的最多点数。
参考了P103的黑书。
View Code
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef struct
{
int x, y;
} point;
bool cmp(point a, point b)
{
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
bool cmp2(point a, point b)
{
if (a.x == b.x) return a.y > b.y;
return a.x < b.x;
}
point a[1000+5];
point b[10000];
int main(void)
{
//freopen("D:/a.txt", "r", stdin);
int n, r;
while (scanf("%d%d", &n, &r)!=EOF)
{
for (int i=0; i<1000; i++)
{
a[i].x = 1000000001;
}
for (int i=0; i<n; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
}
sort(a, a + n, cmp);
int top=0, max = 0;
for (int i=0; i<n; i++)
{
int max2 = 0;
top=0;
int sta = a[i].x, ed = a[i].x + r;
for (int j=0; j<n; j++)
{
if (a[j].x < sta || a[j].x > ed) continue;
b[top].x = a[j].y;
b[top].y = 1;
top++;
b[top].x = a[j].y + r + 1;
b[top].y = -1;
top++;
}
sort(b, b+top, cmp2);
int sum = 0;
for (int i=0; i<top; i++)
{
sum += b[i].y;
max2 = max2 > sum ? max2 : sum;
}
max = max > max2 ? max : max2;
}
printf("%d\n", max);
}
return 0;
}