POJ - 1328 Radar Installation (贪心)

                                            Radar Installation

Time Limit: 1000MS   Memory Limit: 10000K

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 

题意:给你n个岛和雷达的最大探测半径,雷达只能安装在x轴上,问最少用多少个雷达可以探测到所有的岛

解题思路:  每个雷达尽量多的覆盖,即在保证覆盖其已覆盖的岛的情况下,尽量向右多覆盖岛。只需要算出雷达要覆盖每个岛在x轴上的坐标区间,从左向右枚举所有区间,区间重复的岛屿用一个雷达即可。 如图 ,最少需要三个雷达分别放置在红色位置

ACCode

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath> 
#define inf 0x3f3f3f3f
using namespace std;
struct Node {
	int x,y;
	friend bool operator < (const Node t1,const Node t2) {
		if(t1.x == t2.x)
			return t1.y < t2.y;
		return t1.x < t2.x;
	}
};
Node loc[1005];
int main()
{
	int n,d,Case=1;
	while(scanf("%d%d",&n,&d) && (n || d)) {
		bool flag = false;
		for(int i=1;i<=n;i++) {
			scanf("%d %d",&loc[i].x,&loc[i].y);
			if(abs(loc[i].y) > d) flag = true; 
		}
		if(flag) {
			printf("Case %d: -1\n",Case++);
			continue;
		}
		sort(loc+1,loc+1+n);
		double r=inf,tl,tr;
		int tot=1;
		for(int i=1;i<=n;i++) {
			tl = loc[i].x - double(sqrt(d*d-loc[i].y*loc[i].y));	// 当前岛屿要被探测到雷达可放置到最左端的位置 
			tr = loc[i].x + double(sqrt(d*d-loc[i].y*loc[i].y));	// 最右 
			if(tl > r) {
				tot++;
				r = tr;
			}
			else {
				r = min(r,tr);
			}
		}
		printf("Case %d: %d\n",Case++,tot);
	}
	
	return 0; 
}

猜你喜欢

转载自blog.csdn.net/weixin_42765557/article/details/97616424