Poj1328【数学】

Radar Installation

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 48   Accepted Submission(s) : 8
Problem 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.
Poj1328【数学】_第1张图片
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
PKU
#include<stdio.h>
#include<stdlib.h>
#include<math.h> 
struct island
{
	float left, right;
}is[1010];
int cmp(const void *a, const void *b)
{
	return (*(struct island *)a).left > (*(struct island *)b).left?1:-1;
}
int main()
{
	int n, d, num  = 1;
	while(scanf("%d%d", &n, &d) != EOF && ( n || d))
	{
		int x, y, flag = 0;
		for(int i = 1; i <= n; i++)
		{
			scanf("%d%d", &x, &y);
			if(y > d)
			flag = 1;
			float dis;
			dis = sqrt( (float)(d*d - y*y));
			is[i].left =  (float)x - dis;
			is[i].right = (float)x + dis;
		}
		qsort(is + 1, n, sizeof(is[0]), cmp);
		int sum = 1;
		float r;
		r = is[1].right;
		for(int i = 2; i <= n; i++)
		{
			if(r > is[i].right)//下个区间在当前区间内,更改右端值 
			r = is[i].right;
			else if(is[i].left > r)//下个区间的左端点大于当前的右端点 则雷达加一 
			{
				sum++;
				r = is[i].right;
			}
		}
		if(flag)
		printf("Case %d: -1\n", num++);
		else
		printf("Case %d: %d\n", num++, sum);
	}
	return 0;
} 

题意:给出n个点的坐标,y>0,求需要几个圆心在x轴上的圆去覆盖这些点。
思路:求出每个点所能被覆盖的圆的x坐标区间,再将这些区间以左端点进行排序,逐一判断是否需要加圆的个数,其中需要注意的就是当一个区间涵盖在另一个区间中的时候,右端点的值需取小的来判断。

你可能感兴趣的:(c)