Radar installation (Greedy)

Source: Internet
Author: User

Radar installation
Time limit:1000 ms   Memory limit:10000 K
Total submissions:54295   Accepted:12208

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
题意:有一个坐标轴,在x轴的上方时海,下方是陆地,x轴为海岸线,海上有n个小岛,因为种种原因要在海岸线上装雷达,每个雷达的覆盖范围是以r为半径的圆,请用最少的雷达数覆盖所有的小岛,当无法完全覆盖时输出-1;
思路:典型的贪心思想。以小岛为圆心以r为半径做园,计算出圆与x轴的左交点和右交点。左交点为x-sqrt(r*r-y*y),右交点为x+sqrt(r*r+y*y);然后对左交点从小到大排序,令初始雷达为最小岛屿的右交点,如果i点的左交点在雷达的右面,则需要重新装一个雷达,然后令当前点为新雷达的右交点,否则如果i点的右交点在当前雷达的左边,则把当前雷达的位置更新为该点的右交点。
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace std;struct node{    double l,r;}point[1010];int cmp(struct node a,struct node b)//对左交点进行排序;{    return a.l<b.l;}int main(){    int n,r,i;    double x,y,t;//用t来储存当前雷达的位置    int cnt=1;    while(~scanf("%d %d",&n,&r))    {        if(n==0&&r==0)            break;        int ans=1;//记录安装雷达的个数        for(i=0;i<n;i++)        {            scanf("%lf %lf",&x,&y);            point[i].l=x-sqrt(r*r-y*y);//当前点与x轴的左交点            point[i].r=x+sqrt(r*r-y*y);//当前点与x轴的右交点            if(y>r||y<0||r<=0)//列举出三种无法覆盖的情况                ans=-1;        }        sort(point,point+n,cmp);//排序        t=point[0].r;//令初始值为位置最小岛屿的右交点        for(i=1;i<n&&ans!=-1;i++)        {            if(point[i].l>t)            {                ans++;                t=point[i].r;            }           if(point[i].r<t)                {                    t=point[i].r;                }        }        printf("Case %d: %d\n",cnt++,ans);    }    return 0;}


Zookeeper

Radar installation (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.