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
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
出处:優YoU http://user.qzone.qq.com/289065406/blog/1299228061
提示:简单的贪心
正确的算法是:要考虑把雷达站放到哪个位置使得包含雷达的区间最多!
写算法的时候要注意,按海岛的横坐标排序(纵坐标是跟随横坐标,但不能对排序构成任何影响)后,第一个雷达建立在区间的右端,然后依次判断每个区间的左端点,如果在最新建立的雷达右面,那么肯定需要一个雷达,而且也建在区间右端。如果左端点在雷达左面,这个时候要考虑区间的右端在雷达的左面还是右面,如果是右面,那雷达位置就不变,如果在左面,那现在的雷达是覆盖不了的,所以要把雷达放在该区间的右端点!因为这样同时不但能覆盖原来的岛,还能覆盖现在的岛
标准方法是把每个点的放置雷达的区间求出,然后按照区间排序。排好序后,从左至右看,当发现下一个区间的起始点大于前面所有区间的最小结束点的时候,答案加一。忽视前面走过的所有点后,对后面进行相同的操作(从左至右看,当发现下一个区间的起始点大于左边未被忽视的所有区间的最小结束点的时候,答案加一)。直到结束。但是这样做相对较难实现。
#include<iostream>
#include<math.h>
using namespace std;
const int island_max=1000;
int main(void)
{
int i,j;
double x[island_max],y[island_max];
double num,rad;
for(;;)
{
/*Input end*/
cin>>num>>rad;
if(!(num&&rad))break;
static int count=1; //记录case数目
/*read in coordinate*/
bool flag=false;
for(i=0;i<num;i++)
{
cin>>x[i]>>y[i];
if(y[i]>rad)
flag=true;
}
/*case fail*/
if(flag)
{
cout<<"Case "<<count++<<": -1"<<endl;
continue;
}
/*bubble sort*/
//这里由于y要随x连带排序,不能简单地使用 快排qsort
double temp;
for(i=0;i<num-1;i++)
for(j=0;j<num-i-1;j++)
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
temp=y[j];
y[j]=y[j+1];
y[j+1]=temp;
}
double left[island_max],righ[island_max]; //海岛圆在海岸线上的左右交点
for(i=0;i<num;i++)
{
left[i]=x[i]-sqrt(rad*rad-y[i]*y[i]);
righ[i]=x[i]+sqrt(rad*rad-y[i]*y[i]);
}
int radar=1;
for(i=0,temp=righ[0];i<num-1;i++)
if(left[i+1]>temp)
{
temp=righ[i+1];
radar++;
}
else if(righ[i+1]<temp)
temp=righ[i+1];
cout<<"Case "<<count++<<": "<<radar<<endl;
}
return 0;
}