HDU-4007 [Dave] O (N ^ 2) Enumeration

Source: Internet
Author: User

Question meaning:

Give you n points and a square with a side length of R. You can use this square to cover up to several points (points on the square boundary are also counted ).


Note: The side of a square must be parallel to the coordinate axis.


Ideas:

Sorts the Y coordinates of N points, and then O (n) enumerates the bottom boundary of a square. Take out the points of Y coordinate in the upper and lower boundary range of N points. Then, the O (n) complexity is used to find the points that can be overwritten by the side with the length of R. Therefore, the overall enumeration complexity is O (n ^ 2 ).

Code:

/* O (N ^ 2) Enumeration */# include <iostream> # include <cstdio> # include <algorithm> # include <memory. h> # define maxn 1005 # define INF 2e9 # define max (A, B) (A> B? A: B) # define min (a, B) (a <B? A: B) using namespace STD; struct node {int X, Y;} node [maxn]; int XX [maxn], YY [maxn]; int xCNT, ycnt; int N, R; void Init () {int I; for (I = 1; I <= N; I ++) {scanf ("% d ", & node [I]. x, & node [I]. y); YY [I] = node [I]. y;} Sort (yy + 1, YY + 1 + n);} void solve () {int I, j, E, ANS = 0; for (I = 1; I <= N; I ++) {xCNT = 0; For (j = 1; j <= N; j ++) {If (node [J]. y> = YY [I] & node [J]. Y <= YY [I] + r) xx [xCNT ++] = node [J]. x;} Sort (XX, xx + xCNT); XX [xCNT ++] = inf; E = 0; fo R (j = 0; j <xcnt-1; j ++) {While (XX [e] <= XX [J] + r) e ++; ans = max (ANS, (e-j) ;}} printf ("% d \ n", ANS) ;}int main () {While (scanf ("% d ", & N, & R )! = EOF) {Init (); solve ();} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.