Poj 1328 (radar installation) ry + greedy

Source: Internet
Author: User
Tags radar

[Description ]:

Given n small islands and their locations, and entering the radar radiation area, how many radar stations are required to cover all small islands?

[Train of Thought Analysis ]:

The first thing that comes to mind in this question is the use of greedy algorithms, but how can we get greedy? When I started to solve this problem, I had only one idea: to make every radar cover more points, but how to cover more is a typical mathematical problem, I did not think about it. I finally learned how to do it after reading the question on the Internet. Let's take a look at how to create a diagram:

Through this graph, we first use a mathematical knowledge, that is, to create a circle with a small island as the center and a radar radiation range as the center. The circle has an intersection with the X axis, using the intersection as the radar station laying point, the radar station can certainly cover any small island. At the same time, if the circles built on the center of each small island do not overlap, then there must be multiple radar stations, if the right point of the circle and the X axis of a small island is smaller than the right point of the circle of the Center of another small island, the two islands will certainly be covered by the same radar.

Of course, when processing data, we need to sort the data.

[AC code ]:

1 # include <iostream> 2 # include <math. h> 3 # include <algorithm> 4 using namespace STD; 5 # define Max 1005 6 struct radar 7 {8 double left; 9 double right; 10 double X; 11 Double Y; 12} R [Max]; 13 int CMP (radar A, radar B) 14 {15 return. left <B. left; // sort the left point of the circle and the X axis of a small island in ascending order. 16} 17 int main () 18 {19 int COUNT = 1, n, D; 20 while (CIN> N> D & (N | D) 21 {22 int flag = 0, sum = 1; 23 Double X, Y; 24 For (INT I = 0; I <n; I ++) 25 {2 6 CIN> x> Y; 27 if (Y> d) Flag = 1; 28 R [I]. X = x; 29 R [I]. y = y; 30} 31 32 for (INT I = 0; I <n; I ++) 33 {34 R [I]. left = R [I]. x * 1.0-sqrt (D * d-r [I]. y * R [I]. y); // calculates the Left and Right intersections between the circle and the X axis. 35 R [I]. right = R [I]. x * 1.0 + SQRT (D * d-r [I]. y * R [I]. y); 36} 37 sort (R, R + N, CMP); 38 If (FLAG) {cout <"case" <count ++ <": -1 "<Endl; continue;} 39 double temp = R [0]. right; 40 for (INT I = 1; I <n; I ++) 41 {42 if (R [I]. left> temp) 43 {44 sum ++; temp = R [I]. right; // If two circles do not intersection, Then add 1 to the number of radars. 45} 46 else if (R [I]. right <temp) 47 {48 temp = R [I]. right; // if the right point of a circle is smaller than the right point of a circle, the circle is updated. 49} 50} 51 cout <"case" <count ++ <":" <sum <Endl; 52} 53 return 0; 54}

 

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.