解题报告:Radar Installation 一道贪心,不算难

来源:互联网 发布:linux yum安装maven 编辑:程序博客网 时间:2024/05/29 08:07

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 above 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


类型:贪心

 

题意:

给你n个岛的坐标和每个雷达的最大半径,问你最少用多少个雷达将它们全部覆盖。

题意要求岛的坐标必须在x轴上方,同时雷达半径为非负数,但是输入数据没有这个限制,所以记得为这两种情况加上特判。

 

 

思路:

根据给的点还有雷达的半径,其实每个点对应的是一个区间[1.0*x-sqrt(H-l), 1.0*x+sqrt(H-l)],然后就是如何放置雷达。先以左边的坐标升序进行排列,如果左坐标相同则按右坐标降序排列,然后开始遍历,雷达初始点为第一个点的右坐标,遇到右坐标比它小的点,雷达则转移到这个点,如果遇到左坐标比它大的点,说明需要放置新的雷达,ans++,同时新雷达的坐标转移到这个点的右坐标,一直扫到最后一个点得出答案就好了。


AC代码:

#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>using namespace std;int n;struct Node{    double a,b;}node[1005];bool cmp(struct Node A,struct Node B){    if(fabs(A.a-B.a)<1e-12)        return A.b>B.b;    return A.a<B.a;}void c_out(){    for(int i=0;i<n;i++)        printf("%d:x1=%.2f,x2=%.2f\n",i,node[i].a,node[i].b);    system("pause");}int TanXin(){    int ans=1;    sort(node,node+n,cmp);//c_out();    double tem=node[0].b;    for(int i=1;i<n;i++)    {        if(node[i].b<tem)            tem=node[i].b;        else if(node[i].a>tem)        {            ans++;            tem=node[i].b;        }    }    return ans;}int main(){    int h,t=0;    while(scanf("%d%d",&n,&h)==2&&(n||h))    {        int flag=0;        if(h<0)            flag=1;        double H=1.0*h*h;        for(int i=0,x,y;i<n;i++)        {            scanf("%d%d",&x,&y);            int l=y*y;            if(l>H||y<0)                flag=1;            else            {                node[i].a=1.0*x-sqrt(H-l);                node[i].b=1.0*x+sqrt(H-l);            }        }        printf("Case %d: ",++t);        if(flag)        {            printf("-1\n");            continue;        }        int ans=TanXin();        printf("%d\n",ans);    }}


0 0
原创粉丝点击