POJ_1328_Radar Installation(greedy)

Radar Installation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 59449   Accepted: 13394

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.
POJ_1328_Radar Installation(greedy)_第1张图片
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

题意:二维坐标系中有n个点,现在要你用最少的圆(圆心在x轴,半径为d)覆盖所有的点。

分析:贪心题。先预处理出每个点对应的圆心坐标区间,意思就是在这个区间内的任意一个点作为圆心坐标都能覆盖该点。然后根据区间右边界升序排序。然后就是区间有无交集的问题了。如果有交集,说明可以用同一个圆覆盖,如果没交集,ans+=1。

题目链接:http://poj.org/problem?id=1328

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;

const int maxn = 1000 + 5;

struct Point{
    double x;
    double y;
}point[maxn];

int n,k=1;
double d,X,Y;
bool judge;

bool cmp(Point a,Point b){
    return a.y<b.y;
}

void input(){
    judge=true;
    for(int i=0;i<n;i++){
        scanf("%lf%lf",&X,&Y);

        if(fabs(Y)>d || d<=0){
            judge=false;
        }
        else{
            point[i].x=X-sqrt(d*d-Y*Y);
            point[i].y=X+sqrt(d*d-Y*Y);
        }
    }
}

void solve(){
    printf("Case %d: ",k++);

    if(!judge || d<=0){
        printf("-1\n");
        return ;
    }
    sort(point,point+n,cmp);
    int ans=1;
    double maxy=point[0].y;
    for(int i=1;i<n;i++){
        if(point[i].x>maxy){
            maxy=point[i].y;
            ans++;
        }
    }
    printf("%d\n",ans);
    return ;
}

int main(){
    while(scanf("%d%lf",&n,&d)!=EOF){
        if(n==0&&d==0) break;
        input();
        solve();
    }return 0;
}


你可能感兴趣的:(Algorithm,sort,ACM,poj,greedy)