POJ 1328 Radar Installation

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.

解题报告:
明显可以覆盖到i这个island的位置是一个区间,我们可以用\(sqrt(r*r-y_i*y_i)\)算出来,问题就转化为给出N个区间,每次选择一个点消去所有覆盖该点的区间,问选择最少的点的个数
那么我们按l排序,从左往右扫描,如果当前没被消去过,那么就必须得消去,因为前面都处理过了,所以不能依靠前面的消去,然后我们就考虑消去这个区间能够顺便消去哪些区间,贪心一下,随便选择能覆盖到的即可,因为对于两个互不重叠的区间,一次只能选择消去一个,另一个还是得花费一次去消除,所以随意选择一个即可

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=1005;
int n,m,kase=0;bool vis[N];
struct node{
	double l,r;
	bool operator <(const node &pp)const{
		return l<pp.l;
	}
}a[N];
void work()
{
	bool flag=false;
	int x,y,ans=0;double s;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&x,&y);
		if(y>m){
			flag=true;continue;
		}
		else{
			s=sqrt((double)(m+y)*(m-y));
			a[i].l=x-s;a[i].r=x+s;
		}
		vis[i]=false;
	}
	if(flag){printf("Case %d: -1\n",++kase);return ;}
	sort(a+1,a+n+1);
	double L,R;
	for(int i=1;i<=n;i++){
		if(vis[i])continue;
		ans++;L=a[i].l;R=a[i].r;vis[i]=true;
		for(int j=i+1;j<=n;j++){
			if(vis[j])continue;
			if(a[j].l>=L && a[j].l<=R){
				vis[j]=true;
				L=Max(a[j].l,L);
				R=Min(a[j].r,R);
			}
 			if(a[j].r>=L && a[j].r<=R){
				vis[j]=true;
				L=Max(a[j].l,L);
				R=Min(a[j].r,R);
			}
		}
	}
	printf("Case %d: %d\n",++kase,ans);
}

int main()
{
	while(~scanf("%d%d",&n,&m) && n+m)
		work();
	return 0;
}

posted @ 2017-09-09 15:32  PIPIBoss  阅读(117)  评论(0编辑  收藏  举报