POJ 1328 Radar Installation(贪心)

来源:互联网 发布:大股东知乎 编辑:程序博客网 时间:2024/06/06 05:48
Radar Installation
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 75586 Accepted: 16939

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

题目大意:

有一排海岸线,海岸线平行于X轴,海岸线的附近有很多岛屿,对于海岸线给定岛屿的坐标。问题是需要安装一些雷达来监测这些岛屿,每个雷达的监测半径为m,然后需要在海岸线上安装雷达,问你最少需要安装多少个,可以把所有的岛屿都监测到,输出数量,如果做不到,输出-1

题目解析:

根据给定的岛屿坐标,我们可以计算出每个岛屿在海岸线上安装雷达的区间,意思就是在这个区间内的任何地方安装雷达都是可以检测到此岛屿。这样我们根据n个岛屿的坐标就得到了n个不同的区间在海岸线上,然后我们只贪心的去处理这些区间,让每个区间内至少都存在一个雷达就可以了,那么从第一个区间, 选择最右边一个点, 如果不选择第一个区间最右一个点(选择前面的点), 那么把它换成最右的点之后, 以前得到满足的区间, 现在仍然得到满足, 所以第一个区间的最右一个点为贪婪选择, 选择该点之后, 将得到满足的区间删掉,继续更新最右边的点,直到结束。

#include <algorithm>#include <iostream>#include <numeric>#include <cstring>#include <iomanip>#include <string>#include <vector>#include <cstdio>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const LL MAX = 405;const double esp = 1e-6;const double PI = 3.1415926535898;const int INF = 0x3f3f3f3f;using namespace std;struct node{    double l,r;}arr[1005];bool cmp(node a,node b){    return a.l < b.l;}int main(){    int n,Cas = 1;    double m,k,nowx,nowy;    while(~scanf("%d %lf",&n,&m) && (n||m)){        int f = 0;        for(int i=0;i<n;i++){            scanf("%lf %lf",&nowx,&nowy);            if(nowy > m)                f = 1;            arr[i].l = nowx - sqrt(m * m - nowy * nowy);            arr[i].r = nowx + sqrt(m * m - nowy * nowy);        }        sort(arr,arr+n,cmp);        int ans = 1;        k = arr[0].r;        for(int i=0;i<n-1;i++){            if(arr[i+1].l > k){                k = arr[i+1].r;                ans += 1;            }            else if(arr[i+1].r < k){                k = arr[i+1].r;            }        }        if(f)            printf("Case %d: -1\n",Cas++);        else            printf("Case %d: %d\n",Cas++,ans);    }    return 0;}


0 0
原创粉丝点击