Dave
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3524 Accepted Submission(s): 1183
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
题解:这个题是要求正方形与坐标轴平行;可以先确定对于x满足的点数,再找y;可以二分找;
代码:
#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<algorithm> #include<cmath> using namespace std; const int MAXN = 1010; typedef long long LL; struct Point{ int x, y; friend bool operator < (Point a, Point b){ return a.x < b.x; } }; Point dt[MAXN]; int p[MAXN]; int main(){ int N, R; while(~scanf("%d%d", &N, &R)){ for(int i = 0; i < N; i++){ scanf("%d%d", &dt[i].x, &dt[i].y); } sort(dt, dt + N); int ans = 0; for(int i = 0; i < N; i++){ int tp = 0; p[tp++] = dt[i].y; for(int j = i + 1; j < N; j++){ if(dt[j].x - dt[i].x > R) break; p[tp++] = dt[j].y; } sort(p, p + tp); for(int l = 0; l < tp; l++){ int cnt = upper_bound(p + l, p + tp, p[l] + R) - (p + l); ans = max(ans, cnt); } } printf("%d ", ans); } return 0; }