poj 1328 Radar Installation(貪心)

Radar Installation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 55857   Accepted: 12596

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

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

Source


題目大意:在直角座標系中,x軸爲海岸,x軸上方爲海(島嶼在x軸上方),下方爲陸地,告訴你島嶼的座標,以及雷達的探測半徑d,求最小數量的雷達,將島嶼覆蓋,如果不能則輸出-1;

顯然在d<y(島嶼的縱座標)時,不可能有雷達覆蓋他,這是輸出-1;

此題採用貪心策略: 以島嶼i座標(p[i].x,p[i].y)爲圓心,做半徑爲d的圓,交x軸兩個交點,p[i].l記爲左交點,p[i].r記爲右交點,將島嶼按照p[i].l排序,按照排序後從左到右依次選擇圓心所在位置(圓心可以不爲整數),使得該圓能夠覆蓋足夠多的點,首先選擇第一個點的p[i].r作爲圓心記爲cr,然後判斷,當(p[i].l>cr)時,顯然,當前的圓覆蓋不了此點,只能新建一個,ans++,否則當p[i].r<cr時,說明還有更優的選擇,將當前cr更新爲p[i].r;

此外此題還應注意一點:用qsort排序時,下面這樣會導致精度丟失,導致WA
int cmp(const void *a,const void *b)
{
	Point *p1=(Point *)a,*p2=(Point *)b;
	if(p1->l!=p2->l)
	return p1->l-p2->l;
	return p1->r-p2->r;
}
應該這樣寫:
int cmp(const void *a,const void *b)
{
	Point *p1=(Point *)a,*p2=(Point *)b;
	if(p1->l>p2->l)
	return 1;
	if(p1->l<p2->l)
	return -1;
	if(p1->r>p2->r)
	return 1;
	if(p1->r<p2->r)
	return -1;
	return 0;
}

代碼:
#include <iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define N 1010
#define getx2(x) (x)*(x)
#define eps 1e-10
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
struct Point
{
	double l,r;
	int x,y;
}p[N];
void init(int n)
{
	for(int i=0;i<=n;i++)
	{
		p[i].l=p[i].r=p[i].x=p[i].y=0;
	}
}
int cmp(const void *a,const void *b)
{
	Point *p1=(Point *)a,*p2=(Point *)b;
	if(p1->l>p2->l)
	return 1;
	if(p1->l<p2->l)
	return -1;
	if(p1->r>p2->r)
	return 1;
	if(p1->r<p2->r)
	return -1;
	return 0;
}
int main(int argc, char** argv) {
	int n,d,cnt=0,i;
	bool flag;
	while(~scanf("%d%d",&n,&d)&&(n||d))
	{
		flag=true;
		init(n);
	     for(i=0;i<n;i++)	
	     {
	     	scanf("%d%d",&p[i].x,&p[i].y);
	     	if(p[i].y>d)
	     	{
	     		flag=false;
	     		continue;
			 }
	     	p[i].l=p[i].x-sqrt(getx2(d)-getx2(p[i].y));
	     	p[i].r=p[i].x+sqrt(getx2(d)-getx2(p[i].y));
		 }
		 if(!flag)
		 {
		 	printf("Case %d: -1\n",++cnt);
		 	continue;
		 }
		 qsort(p,n,sizeof(p[0]),cmp);
		 int ans=1;
		 double cr=p[0].r;
		 for(i=1;i<n;i++)
		 {
		 	if(p[i].l>cr)
		 	{
		 		ans++,cr=p[i].r;
			 }
			 else if(p[i].r<cr)
			  cr=p[i].r;
		 }
		 printf("Case %d: %d\n",++cnt,ans);
	}
	return 0;
}


發佈了181 篇原創文章 · 獲贊 11 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章