poj 1328 Radar Installation

Radar Installation
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 45575
Accepted: 10157

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.
poj 1328 Radar Installation
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

Beijing 2002

[Submit]   [Go Back]   [Status]   [Discuss]


/*=============================================================================
#     FileName: radar.cpp
#         Desc: 
#       Author: zhuting
#        Email: [email protected]
#     HomePage: my.oschina.net/locusxt
#      Version: 0.0.1
#    CreatTime: 2013-12-03 00:12:22
#   LastChange: 2013-12-03 10:20:49
#      History:
=============================================================================*/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define maxn 1005
using namespace std;

struct line/*区间结构*/
{
	double left;/*左右边界*/
	double right;
};
struct line li[maxn];


bool cmp (struct line a, struct line b)/*sort的比较函数*/
{
	if(a.left == b.left) return b.right < a.right;
	return a.left < b.left;
}

int main()
{
	int i;
	int case_num;
	int n;
	double d;/*尽量都用double,避免错误*/
	double x, y;
	double tmp, l, r;

	double pos;
	int num;
	bool is_possible;/*是否有解*/
	

	case_num = 0;
	while(1)
	{
		++case_num;
		scanf("%d%lf", &n, &d);
		if (!n && !d) break;
		is_possible = 1;
		if (d <= 0) is_possible = 0;/*半径得大于0*/
		for (i = 0; i < n; ++i)
		{
			scanf("%lf%lf", &x, &y);
			if (!is_possible) continue;
			tmp = (d*d - y*y);/*一定大于0*/
			if (tmp < 0 || y < 0)
			{
				is_possible = 0;
				continue;
			}
			tmp = sqrt(tmp);
			l = x - tmp;
			r = x + tmp;
			li[i].left = l;
			li[i].right = r;
		}
		if (!is_possible)
		{
			printf("Case %d: -1\n", case_num);
			continue;
		}

		sort(li, li + n, cmp);

		
		pos = li[0].right;/*一开始,第一个雷达位置为区间0的右边界*/
		num = 1;
		for (i = 1; i < n; ++i)
		{
			if (li[i].left > pos)/*区间左边界在当前雷达的右边,则一定得加一个雷达*/
			{
				++num;
				pos = li[i].right;/*新雷达在区间最右*/
			}
			else if (li[i].right < pos)/*区间右边界在当前雷达左侧,则新雷达移动至现区间右边界*/
				pos = li[i].right;
		}
		printf("Case %d: %d\n", case_num, num);
	}
	return 0;
}



你可能感兴趣的:(poj)