题意

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.

方法

以每个海岛为圆心,以d为半径画圆,与x轴相交形成的区间则是覆盖改点的雷达候选位置。(相交不到则说明无解)

于是此题转化为 区间选点问题 ,即 选最少的点使得每个区间内都有一点

由贪心,将区间按照左端点的大小排序。从左到右,选取一连串区间使得他们有共同的交点,可以认为每组都是取第一个区间最右端的点。这样一组组按顺序找,找到的组数即为答案。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string.h>
using namespace std;
const int MAXN=1000+5;
bool cir[MAXN][MAXN];
int n;
double d;
struct Points{
    double x;
    double y;
}points[MAXN];
bool cmp(Points a, Points b){
    return a.x<b.x;
}
inline double dis(Points a,Points b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void init(){
    memset(cir,false,sizeof(cir));
    for(int i=0;i<n;i++)
        scanf("%lf%lf",&points[i].x,&points[i].y);
    sort(points,points+n,cmp);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            if(points[i].x==points[j].x&&points[i].y!=points[j].y){
                if(max(points[i].y,points[j].y)<=d)cir[i][j]=true;
                else cir[i][j]=false;
                continue;
            }
            if(i==j){
                cir[i][j]=true;continue;
            }
            Points tt;tt.y=0;
            tt.x=(points[j].y*points[j].y-points[i].y*points[i].y+points[j].x*points[j].x-points[i].x*points[i].x)/((points[j].x-points[i].x)*2);
            cout<<"i="<<i<<" j="<<j<<" r1="<<dis(points[i],tt)<<" r2="<<dis(points[j],tt)<<endl;
            if(dis(points[i],tt)<=n)
                cir[i][j]=true;
    }
}
int main(){
    while(scanf("%d%lf",&n,&d)&&n){
        init();
        int stp=0,edp=1;///edp<=
        int ans=0;
        while(edp<=n){
            bool ok=false;
            for(int i=stp;i<edp;i++)
                if(!cir[i][edp]){
                    ans++;
                    stp=edp;
                    edp++;
                    ok=true;
                    break;
                }
            if(ok)continue;
            edp++;
        }
        printf("%d\n",ans);
    }
    
    
}
 posted on 2017-01-29 18:57  cylcy  阅读(178)  评论(0编辑  收藏  举报