Poj 1328 radar installation greedy question

Source: Internet
Author: User
Tags float max radar

This is a greedy question, but you need to observe the law by yourself. This is not easy and error-prone.

Generally, the online method is to find the interval.

Here is a unique method:

1. sort by X axis size

2. From the leftmost vertex loop, first find the circle of the smallest X axis.

3. Use this circle to determine the number of circles on the right until the next vertex cannot be included. Then, continue step 1 and draw a new circle.

The Code should be clear and intuitive.

The efficiency is O (n). Although there is a nested loop, the subscript is not repeated, and the loop can be done once, so it is O (n ).


# Include <stdio. h> # include <cmath> # include <algorithm> # include <float. h> using namespace STD; const int max_n = 1001; inline float max (float a, float B) {return A> B? A: B;} inline float min (float a, float B) {return a <B? A: B;} struct point {float X, Y; bool operator <(const point & P) const {If (x = P. x) return y> P. y; return x <p. x;} float dist (const point & P) const {float a = (X-P. x); float B = (Y-P. y); Return sqrtf (A * A + B * B);} float dist (const float X1, const float Y1) const {float a = (X-X1 ); float B = (Y-Y1); Return sqrtf (A * A + B * B) ;}}; point PS [max_n]; int calradar (INT N, int D) {sort (Ps, PS + n); float cenx = 0.0f, c Eny = 0.0f; int ans = 0; For (INT I = 0; I <n;) // Add I {ans ++ as needed; float dx = sqrtf (float (D * D)-PS [I]. y * PS [I]. y); cenx = Ps [I]. X + dx; for (I ++; I <n & PS [I]. x <= cenx; I ++) {dx = sqrtf (float (D * D)-PS [I]. y * PS [I]. y); cenx = min (cenx, PS [I]. X + dx) ;}float Dis = 0.0f; For (; I <n; I ++) {Dis = Ps [I]. dist (cenx, ceny); If (DIS> float (D) break;} return ans;} int main () {int N, D, T = 1; while (scanf ("% d ", & N, & D) & (N | D) {float Maxy = flt_min; // This is an error! For (INT I = 0; I <n; I ++) {scanf ("% F", & PS [I]. x, & PS [I]. y); Maxy = max (PS [I]. y, Maxy);} If (Maxy> (float) d) printf ("case % d:-1 \ n", t ++ ); else printf ("case % d: % d \ n", t ++, calradar (n, d) ;}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.