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
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
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
此题的贪心思想就是,在能将所需的小岛包进圆的前提下,尽量将圆心右移。
在判断下一个小岛时,如果该小岛是栈顶圆左移能包进去的,则将栈顶的圆心左移,
否则再入栈一个圆心,使该岛到圆心的距离刚好为半径,也就是尽量将圆心右移
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stack>
#include <cmath>
#define zzz e-6
using namespace std;
struct Land
{
int x,y;
}land[1008];
bool cmp(Land a,Land b)//这里的排序,先按横坐标从左到右,再按纵坐标从上到下(只有在上)
{
if(a.x>b.x)
{
return 0;
}
if(a.x<b.x)
{
return 1;
}
if(a.x==b.x)
{
return abs(a.y)>abs(b.y)?1:0;
}
}
int main()
{
int n,d;
int t=0;
while(cin>>n>>d&&n)
{
t++;
bool flag=true;
stack <double> q;
for(int i=1;i<=n;i++)
{
cin>>land[i].x>>land[i].y;
if(abs(land[i].y)>d)
flag=false;
}
sort(land+1,land+n+1,cmp);
int sum=0;
for(int i=1;i<=n;i++)
{
if(q.empty())
{
q.push(sqrt(double(d*d-land[i].y*land[i].y))+land[i].x);
sum++;
continue;
}
if((q.top()-land[i].x)*(q.top()-land[i].x)+land[i].y*land[i].y<=d*d)//如果该岛可被栈顶的圆包围在内
{
continue;
}
if(land[i].x>=q.top())//该岛不能被栈顶圆包围在内,而且该岛在圆心的右边,也就是说
// 圆心左移无法实现将该岛包围起来,所以必须再用个圆。当然,尽量圆心右移
{
q.push(sqrt(double(d*d-land[i].y*land[i].y))+land[i].x);
sum++;
continue;
}
if(land[i].x<q.top())//该岛不能被栈顶圆包围在内,而且该岛在圆心的左边,说明我们刚才太
//贪心了,现在适当左移一下,不过还是得贪心,刚好左移到能把该岛包围住就行
{
q.pop();
q.push(sqrt(double(d*d-land[i].y*land[i].y))+land[i].x);
}
}
cout<<"Case "<<t<<": ";
if(flag)
{
cout<<sum<<endl;
}
else cout<<-1<<endl;
}
return 0;
}