(简单) 不需要算法 HOJ 1019 Grandpa\'s Other Estate
2014-02-08 09:52
176 查看
Grandpa's Other Estate
My Tags | (Edit) |
---|
Source : ACM ICPC Tehran Regional Contest 2002 | |||
Time limit : 5 sec | Memory limit : 32 M |
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[/b]
There should be one line per test case containing the maximum number of trees that Kamran can own.Sample Input[/b]1
3 1
1 2
2 1
4 3Sample Output[/b]
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); }}
相关文章推荐
- ZOJ 1312 题解这是一道简单题但容易出错,不需要使用任和算法就可做出来,而且很快。我的AC 0ms
- 求数根的简单算法
- 捉鸡的十大算法简单学习,整理的相当乱
- 几个简单有趣的算法
- 简单解释 MapReduce 算法
- 字符串之简单匹配算法(Brule-Force算法、BF算法)
- 三道简单算法题(一)
- 简单的瞄准算法之一直接瞄准算法
- 杭电ACM几个简单的算法
- 简单理解计算-算法时间复杂度
- [C++算法]六种简单算法
- DFS深度优先遍历算法简单分析
- 转型AI产品经理,原来不需要学那么深的算法和数学模型
- JAVA实现简单抢红包算法(模拟真实抢红包)
- 用简单直白的方式讲解A星寻路算法原理
- 水下图像增强相关算法的一个简单小结。
- 简单算法之—Joseph
- 简单探讨可牛影像软件中具有肤质保留功能的磨皮算法及其实现细节。
- 【打CF,学算法——二星级】Codeforces 705B Spider Man (简单博弈)