POJ 1328 Radar Installation Greedy

Source: Internet
Author: User
Tags float max min radar

The problem is greedy method, but it is necessary to observe the law, which is not easy, it is easy to make mistakes.

The general online approach is to find the way to the interval.

Here's a unique approach:

1 Sort by x-axis size

2 from the leftmost point loop, first find the smallest x-axis circle

3 Judging by this circle can include the number of circles on the right, until the next point can not be included, then proceed to the 2nd step and draw a new circle.

Look at the code, it should be very clear and intuitive.

Efficiency is O (n), although there is a nested loop, but the subscript is not duplicated, once again the loop can be, therefore 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, ceny = 0.0f;
	int ans = 0;
		for (int i = 0; i < n;)//Increase I {ans++ as appropriate;
		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%d", &n, &d) && (n | | d)) {Float Maxy = flt_min;//This put out, error.
			for (int i = 0; i < n; i++) {scanf ("%f%f", &ps[i].x, &AMP;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.