Radar Installation POJ – 1328(贪心)

  • Post author:
  • Post category:其他




Radar Installation POJ – 1328

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


题意:


给出n给点坐标,以及雷达的半径d(雷达在x轴上),求覆盖所有点的最小雷达数目,若是不可以,则输出-1。


题解


转化问题,将2维的转化为1维。

将每个点的雷达可包含的区间表示出来。

雷达表示为一个点,如果点在区间内则满足要求。

使点最少,就变成了,点覆盖的区间数尽量大。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
struct P {
	double x, y, s, t, k;//k表示是否判断过
};
bool cmp (P a, P b) {
	return a.t < b.t;
}
int main() {
	int n;double d;
	int m=1;
	while (cin >> n >> d) {		
		int ans=0;
		if (n == 0 && d == 0)break;
		P a[1011];//区间
		int flag=1;
		for (int i = 0; i < n; i++ ) {
			cin >> a[i].x >>a[i].y;
			if(a[i].y>d || d<=0 || a[i].y < 0){
				flag = 0;
			}
			a[i].s = a[i].x + sqrt( d*d - (a[i].y)*(a[i].y));
			a[i].t = a[i].x - sqrt( d*d - (a[i].y)*(a[i].y));
			if(a[i].s > a[i].t) swap (a[i].s, a[i].t);
			a[i].k = 1;
		}
		sort(a, a+n, cmp);
       		for (int i = 0; i < n; i++ ) {
			if ( a[i].k == 1 ){
               			ans++;
				a[i].k=0;
				for(int j = i + 1;j < n; j++)
				{
					if(a[i].t >= a[j].s)
                        		a[j].k=0;
				}
			}
		}
		if(flag==1)  cout<<"Case "<<m++<<": "<<ans<<endl;
        	else cout << "Case " << m++ << ": " << -1 << endl;
	}
}



版权声明:本文为rainbowsea_1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。