(简单) 不需要算法 HOJ 1019 Grandpa\'s Other Estate

来源:互联网 发布:碧柔防晒知乎 编辑:程序博客网 时间:2024/06/05 19:07

Grandpa's Other Estate

My Tags  (Edit)
Source : ACM ICPC Tehran Regional Contest 2002Time limit : 5 secMemory limit : 32 M

Submitted : 596, Accepted : 272

From our previous contest, we know that Kamran the Believer inherited many of his grandpa's belongings. Apparently, his grandpa had been a mathematician in his life with interests in puzzle solving, since he has made Kamran solve another programming problem!

Grandpa had a big garden with many valuable walnut trees. He has written in his will that Kamran can inherit one piece of square shaped land of a given size in the garden, such that its sides be parallel to the x and y axes. Taking advantage of the fact that no other restrictions have been mentioned in the will, Kamran wants to choose the land in which the most number of trees lie. Kamran is too wealthy now and thus too lazy to spend time and solve another algorithmic problem. He has hired you to solve this problem for him.

You are given the location of all trees in the big garden and the size of the land to choose. You are to write a program to find out where to choose the land so that the most number of trees lie in it. You may consider trees as points in the plane and the land as a square. You are to find the position of the square such that it includes as many points as possible. Note that the points on the border of the square are considered to be inside it.

Input
The first line of the input file contains a single integer t (1<=t<=10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1<=n<=100), the number of trees, and an integer r (1<=r<=1000), the length of the land's side, followed by n lines, each containing two integers x and y (0<=x , y <= 100,000) representing the coordinates of a walnut tree. Note that all coordinates are pairwise distinct.

Output
There should be one line per test case containing the maximum number of trees that Kamran can own.

Sample Input

13 11 22 14 3
Sample Output
2

题意:给出最多100个点的坐标,然后要画一个边长为r的正方形,怎么画能包括最多的点。

思路:数据量不大就枚举吧,首先100个点最多有100个不同的x坐标和100个不同的y坐标,而选取的正方形一定会有点在他的边上,只要正方形的边上没有点,我们可以通过移动正方形的同时不把已包括的点排除,但可能会包括进新的点。所以最佳的正方形画法一定会有点在边上。然后我们开始枚举,怎么枚举呢?我们把点中出现了的x作为left或者right,y作为top或者bottom,r是已知的,然后遍历一下看有多少个点在正方形内就可以了,然后在所有的可能中去最大的值就是最大的。

代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 100+10;
struct Point
{
int x;
int y;
}pt[maxn];

int X[maxn];
int Y[maxn];

int T , R , n;
bool cmp(Point p1,Point p2)
{
if (p1.x==p2.x) return p1.y<p2.y;
return p1.x<p2.x;
}

int Bisearch_l(int left,int right,int x)
{
int mid = (left+right)>>1;
while (left<=right)
{
if (pt[mid].x < x) left = mid+1;
else right = mid-1;
mid = (left+right)>>1;
}
return left+1;
}

int Bisearch_r(int left,int right,int x)
{
int mid = (left+right)>>1;
while (left<=right)
{
if (pt[mid].x <= x) left = mid+1;
else right = mid-1;
mid = (left+right)>>1;
}
return left-1;
}

int Try(int ver,int hor)
{
int ret1 = 0;
int ret2 = 0;
int ret3 = 0;
int ret4 = 0;
int p = Bisearch_l(0,n-1,ver);
int p1 = p;
while ( p < n && pt[p].x <= R+ver)
{
if (pt[p].y <= hor+R && hor <= pt[p].y) ++ret1;
if (hor-R <= pt[p].y && pt[p].y <= hor) ++ret3;
++p;
}
p = Bisearch_r(0,n-1,ver);
while (p >= 0 && pt[p].x >= ver-R)
{
if (pt[p].y <= hor+R && hor <= pt[p].y) ++ret2;
if (hor-R <= pt[p].y && pt[p].y <= hor) ++ret4;
--p;
}
return max(ret1,max(ret2,max(ret3,ret4)));
}

int main()
{
cin>>T;
while (T--)
{
scanf("%d%d",&n,&R);
for (int i = 0 ; i < n ; ++i)
{
scanf("%d%d",X+i,Y+i);
pt[i].x = X[i];
pt[i].y = Y[i];
}
sort(pt,pt+n,cmp);
int ans = 0;
for (int i = 0 ; i < n ; ++i)
{
for (int j = 0 ; j < n ; ++j)
{
ans = max(ans,Try(X[i],Y[j]));
}
}
printf("%d\n",ans);
}
}
0 0
原创粉丝点击