hdu 4007 Dave(线性探查+枚举)
Problem Description
Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there are N (1<=N<=1000) people on the ground. Now he wants to know how many people will be in a square with the length of R (1<=R<=1000000000). (Including boundary).
Input
The input contains several cases. For each case there are two positive integers N and R, and then N lines follow. Each gives the (x, y) (1<=x, y<=1000000000) coordinates of people.
Output
Output the largest number of people in a square with the length of R.
Sample Input
3 2 1 1 2 2 3 3
Sample Output
3
Hint
If two people stand in one place, they are embracing.
Source
题目意思:
给你N个点和一个边长为R的正方形,为你用这个正方形最多可以覆盖几个点(在正方形边界上的点也算)。
注意:正方形的边一定平行于坐标轴。
思路:
对N个点的y坐标进行排序,然后O(N)枚举正方形的下边界。取出N个点中y坐标在上下边界范围内的点。然后O(N)复杂度求出长度为R的边最多可以覆盖的点。所以整体枚举的复杂度为O(N^2)。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<cmath> 7 #include<stdlib.h> 8 #include<map> 9 using namespace std; 10 #define inf 1<<30 11 #define N 1006 12 int n,r; 13 struct Node{ 14 int x,y; 15 }p[N]; 16 int yy[N]; 17 int xx[N]; 18 int main() 19 { 20 while(scanf("%d%d",&n,&r)==2){ 21 for(int i=0;i<n;i++){ 22 scanf("%d%d",&p[i].x,&p[i].y); 23 yy[i]=p[i].y; 24 } 25 sort(yy,yy+n); 26 27 int ans=0; 28 for(int i=0;i<n;i++){ 29 //int y=yy[i]; 30 int xcnt=0; 31 for(int j=0;j<n;j++){ 32 if(p[j].y<=yy[i]+r && p[j].y>=yy[i]){ 33 xx[xcnt++]=p[j].x; 34 } 35 } 36 sort(xx,xx+xcnt); 37 38 int e=0; 39 //xx[xcnt++]=inf; 40 for(int j=0;j<xcnt;j++){ 41 while(xx[e]-xx[j]<=r && e<xcnt) e++; 42 ans=max(ans,e-j); 43 } 44 45 } 46 printf("%d\n",ans); 47 48 49 50 } 51 return 0; 52 }