Poj 1328 radar installation (Classic greedy)

Source: Internet
Author: User
Tags radar

Radar installation
Time limit:1000 ms   Memory limit:10000 K
Total submissions:54143   Accepted:12178

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

Algorithm analysis:

At the beginning, I don't know where to analyze it. Then I found that the center of the circle is a breakthrough. First, I got the center position corresponding to each point. Note that if you want to cover the most, every circle tries its best to make the point exactly at the circle boundary. For example, if we have a fruit on both sides of the circle, we are not sure if we can get two. In order to make it possible to get two, we can make the left hand touch one, stretch the right hand to grasp another, instead of directly taking one as the center and ignoring the opportunity to increase itself to be closer to the other, so the problem becomes the center of the solution, sort by the center of the radar and update the center of the radar continuously to minimize the number of radar centers. In addition, pay attention to the code comments section...

The vertical axis of the center is 0, and the horizontal axis coordinate calculation formula is as follows:

R = x + SQRT (D * D-y * Y) // move the center from left to right.

# Include <iostream> # include <algorithm> # include <cmath> using namespace STD; typedef struct island {int X; int y; Double Z;} Island; island land [1000]; int comp (Island A, island B) {return. z <B. z;} int main () {int N, D, I, Count, num = 1, sign; while (CIN> N> d) {Sign = 0; if (! (N | D) break; Count = 1, I = 0; while (I <n) {CIN> land [I]. x> land [I]. y; land [I]. z = (double) land [I]. X + SQRT (double (D * D-land [I]. y * Land [I]. y); // The center must be a floating point. If (ABS (land [I]. y)> d | D <= 0) // Note: When the radar cannot be enveloped, output-1 Sign = 1; I ++;} If (sign) {cout <"case" <num <":-1" <Endl; num ++; continue;} Sort (land, land + N, comp ); // sort the center position from left to right. I = 1; Double A = land [0]. z; while (I <n) {If (! (Land [I]. x-a) * (land [I]. x-A) + land [I]. y * Land [I]. Y <= D * D) {A = land [I]. z; count ++;} I ++;} cout <"case" <num <":" <count <Endl; num ++ ;} return 0 ;}


Poj 1328 radar installation (Classic greedy)

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.