POJ 1328 Radar Installation

POJ 1328 Radar Installation(贪心)

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 101806 Accepted: 22654

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.
img
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 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

题意

  以x轴为分界,y>0部分为海,y<0部分为陆地,给出一些岛屿坐标(在海中),再给出雷达可达到范围,雷达只可以安在陆地上,问最少多少雷达可以覆盖所以岛屿。

解题思路

  首先我们可以先判断,因为圈的圆心是在x轴上的,如果有点的纵坐标大于半径d,则直接输出“-1”。再看这题,很明显没什么能直接求解的方法,我们需要转换一下。我们可以将每个点在x轴上能被圈住的区域求出来,然后可以直接排序,排序也是有讲究的,按照右端点的升序。类似于活动安排问题,每次维护一个右界,每个区间就相当于一个整圆的范围,这样就可以直接贪心。

代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
const int maxn = 1005;

struct node
{
    double x,y;
} arr[maxn];
int n,d;

bool cmp(node a,node b)
{
    if(a.y!=b.y) return a.y<b.y;
    return a.x>b.x;
}
void solve(node a,int i)
{
    double dis=sqrt(d*d-a.y*a.y);
    arr[i].x=a.x-dis;
    arr[i].y=a.x+dis;
}
int main()
{
//    freopen("in.txt","r",stdin);
    int k=1;
    while(cin>>n>>d)
    {
        if(n==0&&d==0) break;
        int flag=0;
        printf("Case %d: ",k++);
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&arr[i].x,&arr[i].y);
            if(arr[i].y>d) flag=1;
        }
        if(flag)
        {
            puts("-1");
            continue;
        }
        for(int i=0; i<n; i++)
            solve(arr[i],i);
        sort(arr,arr+n,cmp);
        double tmp=-0x3f3f3f3f;
        int ans=0;
        for(int i=0; i<n; i++)
        {
            if(tmp<arr[i].x)
            {
                tmp=arr[i].y;
                ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36258516/article/details/80215594