[ACM] poj 1328 radar installation (greedy, range selection problem)

Source: Internet
Author: User
Tags radar

Radar installation
Time limit:1000 ms   Memory limit:10000 K
Total submissions:51131   Accepted:11481

Description

Assume the coasting is an infinite straight line. land is in one side of coasting, sea in the other. each small island is a point locating in the sea side. and any radar installation, locating on the coasting, can only cover D distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most D.

We use Cartesian coordinate system, defining the coasting is the x-axis. the sea side is abve X-axis, and the land side below. given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. note that the position of an island is represented by its X-Y coordinates.

Figure A sample input of radar installations

Input

The input consists of several test cases. the first line of each case contains two integers n (1 <= n <= 1000) and D, where N is the number of islands in the sea and D is the distance of coverage of the radar installation. this is followed by n lines each containing two integers representing the coordinate of the position of each island. then a blank line follows to separate the cases.

The input is terminated by a line containing pair of zeros

Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" Installation means no solution for that case.

Sample Input

3 21 2-3 12 11 20 20 0

Sample output

Case 1: 2Case 2: 1

Source

Beijing 2002


Solution:

In a coordinate system, X represents the coast, N points above the X axis represent N Islands, and radar is built on the X axis, the coverage range of a radar is the circle with a radius of D. The minimum number of radars on the X axis can be used to cover all the islands. If not, output-1.

The interval point selection problem is given n closed intervals. It is recommended that the minimum vertex be selected in the interval so that each interval contains at least one vertex (one vertex can be in different intervals ). For example.

The greedy policy is to sort n intervals first by the right endpoint from small to large. If the endpoint is the same, it is sorted by the Left endpoint from large to small. Select the right endpoint of the first interval. If the left endpoint of a later interval is greater than the selected endpoint value, the selected endpoint value is changed to the right endpoint value of the later interval. For example, if the current temp is 23, a left endpoint of the interval is 25 and the right endpoint is 27, so temp = 27. (A new vertex is selected)

Back to this question, we need to build the shortest radar. First, draw a circle on N Islands, and there are two intersections with the X axis, so that each island corresponds to a range (as long as the radar is built in this range, the island will certainly be covered ). The equation of the circle (X-A) 2 + (Y-B) 2 = R2. In this question, B is always 0 because it is on the X axis. The condition for determining non-solution is r <Y. We get n intervals, which translates into selecting as few points as possible in the N intervals, so that each interval has at least one point in the N intervals. Follow the above method.

Refer:

Http://blog.csdn.net/dgq8211/article/details/7534776


Code:

# Include <iostream> # include <string. h >#include <algorithm> # include <cmath> using namespace STD; const int maxn = 1010; struct n {double L, R;} interval [maxn]; bool CMP (n a, n B) // sort {if (. r <B. r) return true; else if (. R = B. r) {if (. l> B. l) return true; return false;} int main () {Double X, Y; int N, D; int c = 1; while (CIN> N> D & (N | D) {bool OK = 1; for (INT I = 1; I <= N; I ++) {CIN> x> Y; double temp = D * D-y * Y; If (temp <0 | D <0) // you need to determine whether D is less than 0, if temp = D-y is written like this, you don't have to judge whether D is less than 0 !! OK = 0; else if (OK) {interval [I]. L = x-SQRT (double) D * D-(double) y * Y); interval [I]. R = x + SQRT (double) D * D-(double) y * Y) ;}} if (! OK) {cout <"case" <C ++ <":" <-1 <Endl; continue;} Sort (interval + 1, interval + 1 + N, CMP); int CNT = 1; double temp = interval [1]. r; For (INT I = 2; I <= N; I ++) {If (temp <interval [I]. l) {CNT ++; temp = interval [I]. r ;}}/* For (INT I = 2; I <= N; I ++) {If (interval [I]. l> temp) {CNT ++; temp = interval [I]. l ;}} * // This is written at the beginning. For example, if the interval [1, 2] [] [6, 9] is incorrect, select the 2, 8 points, in the preceding example, is selected. Obviously, cout <"case" <C ++ <":" <CNT <Endl;} return 0;} is not supported ;}




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.