[POJ1328]Radar Installation

[POJ1328]Radar Installation

试题描述

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

 

输入

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 

输出

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.

输入示例

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

输出示例

Case 1: 2
Case 2: 1

数据规模及约定

见“输入

题解

每个点被观测到的条件是它与任意一个岸上的观测站的距离不超过 d,所以以每个点为圆心,d 为半径画圆,与 x 轴交于两点,这两点构成的一条线段即观测站放在这条线段中就能满足这个点。于是转化成了区间选点问题,经典的贪心问题。

注意考虑无解的情况。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
	return x * f;
}

#define maxn 1010
struct Line {
	double l, r;
	Line() {}
	Line(double _, double __): l(_), r(__) {}
	bool operator < (const Line& t) const { return r < t.r; }
} ls[maxn];

const double eps = 1e-6;

int main() {
	int n, d, kase = 0;
	while(scanf("%d%d", &n, &d) == 2) {
		if(!n && !d) break;
		bool ok = 1;
		for(int i = 1; i <= n; i++) {
			int x = read(), y = read();
			if(y < 0 || y > d) ok = 0;
			double l = sqrt((double)d * d - y * y);
			ls[i] = Line((double)x - l, (double)x + l);
		}
		sort(ls + 1, ls + n + 1);
		double last = -1e9;
		int ans = 0;
		for(int i = 1; i <= n; i++)
			if(ls[i].l > last) ans++, last = ls[i].r;
		if(!ok) ans = -1;
		printf("Case %d: %d\n", ++kase, ans);
	}
	
	return 0;
}

 

posted @ 2016-11-26 10:05  xjr01  阅读(233)  评论(0编辑  收藏  举报