Poj 1328 radar installation greedy Problem Solving

Source: Internet
Author: User
Tags radar

Link: poj 1328 radar installation

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

Question

Radar needs to be arranged on the coastline indicated by the X axis to cover N Islands in the sea. The radar coverage radius is d, and how to select the arrangement points to use the least radar. If some islands cannot be overwritten, output-1.

Analysis

Look at the area with the island as the center and the circle with the D radius to see the intersection with the X axis. In this way, radar can be arranged at any position in the area to cover the island, for all the islands, of course, there are some coincidence cases in the desired range. Then, in this overlap range, the radar will certainly be able to cover more than two points, in this way, the more radar ranges, the more radar saved.


Thoughts

Typical greedy thoughts. We sort the obtained intervals so that the right endpoint of the interval is smaller than the previous one. If the right endpoint is equal, the left endpoint is larger than the previous one (think about why ).

So how should we select the layout point?

First, we select the right endpoint of the first interval after the order number as the first layout point, and its position is St, and then we can find the subsequent range in order. If the left endpoint of the current range is greater than the St, it means that the radar at the St position cannot cover this island. Then, the radar number plus 1 and the ST are updated to the right endpoint of the range. If the left endpoint of the current range is less than or equal to St, it means that the radar at the St position can overwrite this island and ignore this range.

Code

/*    POJ_1328_Radar_Installation    Author: Sign_    Greedy*/#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;int n, d;struct seg{    double l, r;}SEG[1010];seg pos(int x, int y){    seg s;    s.l = x - sqrt(d*d - y*y);    s.r = x + sqrt(d*d - y*y);    return s;}bool cmp(seg a, seg b){    if(a.r == b.r) return a.l > b.l;    return a.r < b.r;}int main(){    int x[1010], y[1010], cas = 1;    while(scanf("%d%d", &n, &d), n, d)    {        bool flag = true;        for(int i = 0; i < n; i++)        {            scanf("%d%d", &x[i], &y[i]);            if(y[i] > d)                flag = false;        }        if(!flag)        {            printf("Case %d: -1\n", cas++);            continue;        }        for(int i = 0; i < n; i++)            SEG[i] = pos(x[i], y[i]);        sort(SEG, SEG+n, cmp);        int cnt = 1;        double st = SEG[0].r;        for(int i = 1; i < n; i++)            if(SEG[i].l > st)            {                st = SEG[i].r;                cnt++;            }        printf("Case %d: %d\n", cas++, cnt);    }    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.