HOJ 1019 Grandpa's Other Estate
2013-04-02 22:25
155 查看
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
Sample Output
题解:
只需将正方形从左到右,从上到下扫描一遍,更新res, 即可
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
1 3 1 1 2 2 1 4 3
Sample Output
2
题解:
只需将正方形从左到右,从上到下扫描一遍,更新res, 即可
#include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> using namespace std; struct point { int x,y; }; point p[105]; bool judge(int x1,int x2,int y2,int y1,point a){ if((a.x - x1) * (a.x - x2) <= 0 && (a.y - y1) * (a.y - y2) <= 0) return true; return false; } int main(){ int t,n,r; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&r); for(int i = 1; i <= n; i++) scanf("%d%d",&p[i].x,&p[i].y); int res = 1; int temp; for(int i = 1; i <= n; i ++) for(int j = i + 1; j <= n; j ++){ if (abs(p[i].x - p[j].x) > r || abs(p[i].y - p[j].y) > r)continue; int tx = min(p[i].x,p[j].x); int ty = max(p[i].y,p[j].y); temp = 0; for(int k = 1; k <= n; k++) if(judge(tx,tx + r, ty , ty - r,p[k])) temp ++; res = max(res,temp); } printf("%d\n",res); } return 0; }
相关文章推荐
- hoj 1560 - Factovisors
- POJ 1019 Number Sequence
- hoj 1014题
- POJ_1019_Number Sequence
- TV Battle --HOJ 11915
- PKU ACM-1019题 java Number Sequence
- HOJ 1020
- HDU-1019 Least Common Multiple
- POJ-1019 Number Sequence
- hoj1956 square
- bjtuOJ1019 Robot
- WA : csu1019 simple line editor
- hoj 1533 Fibonacci Numbers
- hoj 1633 Transmitters //poj 1106 Transmitters
- hoj 1520 The Bottom of a Graph // poj 2553 The Bottom of a Graph
- [割点问题]HOJ 12307 Disconnected Pair
- poj 2197 hoj 2027 Jill's Tour Paths 搜索
- hoj 2648 GSM 凸包
- HOJ 1456 Team Queue(用queue进行模拟)
- URAL 1019 Line Painting(解题报告)