Radar Installation(poj1328)(贪心)

Radar Installation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 54756   Accepted: 12337

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
/*思路:该题题意是为了求出能够覆盖所有岛屿的最小雷达数目,
每个小岛对应x轴上的一个区间,在这个区间内的任何一个点放置雷达,
则可以覆盖该小岛,区间范围的计算用[x-sqrt(d*d-y*y),x+sqrt(d*d-y*y)];
问题即转化为已知一定数量的区间,求最少区间可以包含所有的点,
使得每个区间内斗至少存在一个点。每次迭代对于第一个区间,
选择最右边一个点, 因为它可以让较多区间得到满足,选择该点之后,
将得到满足的区间删掉, 进行下一步迭代, 直到所有的点均包含则结束。
不过还要考虑d<0和y>d的情况。
左端点:表示雷达能够覆盖岛左临界的雷达,
右端点:表示雷达能够覆盖岛右临界的雷达。
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct st
{
	double left,right;
}data[1010];
int cmp(st a,st b)
{
	return a.left<b.left;
}
int main()
{
	int i,t,kase=1,sum,n,d;
	double x,y,ans;
	while(scanf("%d%d",&n,&d)&&(n||d))
	{
		t=0;
		for(i=0;i<n;i++)
		{
		    scanf("%lf %lf",&x,&y);
		    if(y>d||d<0)
		    {
		    	t=1;
		    }
		    data[i].left=x-sqrt(d*d-y*y);
		    data[i].right=x+sqrt(d*d-y*y);
		}
		sort(data,data+n,cmp);
		if(t==1)
		{
		    printf("Case %d: -1\n",kase);
		    kase++;
		}
		else
		{
			ans=data[0].right;
			sum=1;
			for(i=1;i<n;i++)
			{
				if(data[i].right<=ans)//如果右端点在前一雷达覆盖范围,
				     ans=data[i].right;//则雷达要放在放在右端点。
				else if(data[i].left>ans)//如果左端点不在前一雷达覆盖范围内,
				{                         //则需要增加新雷达,并将雷达放在右端点。
				    sum++;
				    ans=data[i].right;
				}
			}
			printf("Case %d: %d\n",kase,sum);
			kase++;
		}
	}
	return 0;
} 

时间: 2024-10-31 14:43:29

Radar Installation(poj1328)(贪心)的相关文章

POJ1328 Radar Installation 【贪心&amp;#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 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 loca

poj1328 Radar Installation(贪心)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1328 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 th

POJ1328 Radar Installation 【贪心&#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 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 loca

poj 1328 Radar Installation (贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52823   Accepted: 11883 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 loca

POJ 1328 Radar Installation 【贪心 区间选点】

解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的位置在x轴上,所以我们设雷达的坐标为(x,0),对于任意一个岛屿p(a,b),因为岛屿要满足在雷达的覆盖范围内,所以 (x-a)^2+b^2=r^2,解得 xmin=a-sqrt(r*r-b*b);//即为区间的左端点 xmax=a+sqrt(r*r-b*b);//即为区间的右端点 接下来区间选点即

poj 1328 Radar Installation 【贪心】【区间选点问题】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54798   Accepted: 12352 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 loca

poj 1328 Radar Installation【贪心区间选点】

Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submission(s) : 22   Accepted Submission(s) : 9 Problem Description Assume the coasting is an infinite straight line. Land is in one side of coa

zoj1360/poj1328 Radar Installation(贪心)

对每个岛屿,能覆盖它的雷达位于线段[x-sqrt(d*d-y*y),x+sqrt(d*d+y*y)],那么把每个岛屿对应的线段求出来后,其实就转化成了经典的贪心法案例:区间选点问题.数轴上有n个闭区间[ai,bi],取尽量少的点,使得每个区间内都至少有一个点.选法是:把区间按右端点从小到大排序(右端点相同时按左端点从大到小),然后每个没选的区间选最右边的一个点即可. #include<iostream> #include<cstdio> #include<cstdlib>

poj1328 Radar Installation 区间贪心

题目大意: 在X轴选择尽量少的点作为圆心,作半径为d的圆.使得这些圆能覆盖所有的点. 思路: 把每个点都转化到X轴上.也就是可以覆盖这个点的圆心的位置的范围[a,b].然后按照每个点对应的a从小到大排序.第一点需要特殊处理,我们赋值r=b0 .也就是使得第一个圆的圆心的横坐标尽量大.然后遍历剩下的点.对于i点,如果该点的ai大于r, 就需要增加一个圆,圆心为bi :否则的话,把r更新为r与bi中小的值. 代码: 1 #include<iostream> 2 #include<cstdio

POJ 1328 Radar Installation(贪心)

http://poj.org/problem?id=1328 题意: 假设滑行是无限直线.土地在海岸的一边,海在另一边.每个小岛是位于海边的一个点.位于海岸上的任何雷达装置只能覆盖距离,所以如果两者之间的距离最大为d,则海中的岛屿可以被半径装置覆盖. 我们使用笛卡尔坐标系,定义惯性是x轴.海侧在x轴之上,陆侧在下.考虑到每个岛屿在海洋中的位置,并且考虑到雷达装置的覆盖距离,您的任务是编写一个程序,以找到覆盖所有岛屿的最少数量的雷达装置.注意,岛的位置由其xy坐标表示. 思路: 由于雷达只能处于x