Poj1328 radar installation (Greedy)

Source: Internet
Author: User

Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents

Question link: http://poj.org/problem? Id = 1328


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

Idea: Reverse Thinking: record the left and right ranges that each island can reach with struct, and use greedy ideas to calculate the number of radars;

The Code is as follows:

# Include <iostream> # include <algorithm> # include <cmath> using namespace STD; struct point {Double R, L;} p [1017]; bool CMP (point, point B) {return. L <B. l ;}int main () {int n, m, R, flag; int I, j; int X, Y; int CAS = 0, cont; while (CIN> N> r) {If (n = 0 & R = 0) break; flag = cont = 0; for (I = 0; I <n; I ++) {CIN> x> Y; If (FABS (y)> r) {flag = 1;} p [I]. L = x * 1.000-sqrt (R * r * 1.00-y * 1.000); P [I]. R = x * 1.000 + SQRT (R * r * 1.00-y * 1.000);} cout <"case" <++ CAS <":"; if (FLAG) {cout <"-1" <Endl; continue;} Sort (p, p + N, CMP); cont = 1; double T = P [0]. r; for (I = 1; I <n; I ++) {If (P [I]. r <t) // to prevent the right section of the previous Island from being greater than the right section of the next island {// if the right section of the island is on the left of the radar, it requires the location of the Mobile Radar to the right of the island T = P [I]. r;} If (P [I]. l> T) // If the left area of the next island is on the right side of the radar {// you need to have another radar located on this island. t = P [I]. r; cont ++ ;}}cout <cont <Endl ;}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.