Poj 1328 radar installation greedy

Source: Internet
Author: User
Tags radar

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

Radar installation
Time limit:1000 ms   Memory limit:10000 K
Total submissions:52768   Accepted:11867

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: 2 Case 2: 1

Source

Beijing 2002


Source code:

# Include <iostream> # include <cmath> # include <string. h >#include <algorithm> # include <cstdio> # include <vector> # include <utility> using namespace STD; const int maxn = 1000 + 5; int main () {int n, k = 1; double D; while (scanf ("% d % lf", & N, & D) & (n! = 0 | D! = 0) {vector <pair <double, double> st; bool vis [maxn]; memset (VIS, false, sizeof (VIS); Double X, Y, l, R; bool flag = false; For (INT I = 0; I <n; ++ I) {scanf ("% lf", & X, & Y); If (Y> d) Flag = true; If (! Flag) {L = x-SQRT (D * D-y * Y); r = x + SQRT (D * D-y * Y); ST. push_back (make_pair (R, L) ;}} if (FLAG) {printf ("case % d:-1 \ n", K ++); continue ;} int COUNT = 0; sort (St. begin (), St. end (); memset (VIS, false, sizeof (VIS); For (INT I = 0; I <St. size (); ++ I) {If (vis [I]) continue; ++ count; r = sT [I]. first; vis [I] = true; For (Int J = I + 1; j <St. size (); ++ J) if (ST [J]. second <= r) vis [J] = true;} printf ("case % d: % d \ n", K ++, count );}}

For each island, there is a radar covering its range on the X axis to find the least radar, so that there is at least one radar in the range corresponding to all islands.

Sort these intervals in ascending order of the right endpoint. Select to build a radar at the right endpoint for selection and removal. For example, if the first interval is [L, R], the left endpoint smaller than R in the remaining interval is removed.

And so on ......



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.