题目链接:点击跳转
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.
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
解题思路:
不知道为什么快排后每次取能覆盖到当前点的最右边的点,遍历查看和之后的点的距离在不在d之内,不在的话sum++,然后更新点的位置,这个一直过不去,然后改了算法,记录x轴上能到每个点的左右区间,按左边的点排序,然后从第一个点开始,每次判断下一个点的左区间是不是比当前取的点的右坐标小,小的话就不需要增加雷达,但是如果当前点的右区间比当前右区间小,就得更新当前右区间为小的值,反之如果当前点的右坐标比当前取得岛的左边左边坐标要小了,那就更新当前点的右坐标为当前判断的岛的右坐标,同时总数加加,最后输出即可(-1的情况是y坐标的绝对值大于覆盖半径d了,这种情况无论如何都无法覆盖到)
代码如下:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const double eps=0.000001;
struct node{
double a,b;
friend bool operator < (const node &o1,const node &o2){
if(o1.a!=o2.a)return o1.a<o2.a;
return o1.b>o2.b;//不排也没事,不影响结果
}
}p[2005];
int main(){
ios::sync_with_stdio(false);
int n, d, ans = 1;
while (cin >> n >> d && (n | d) != 0) {
bool f=false;
for(int i=0;i<n;i++){
double a1,b1;
cin>>a1>>b1;
if(fabs(b1)>d){
f=true;
}else{
double res=sqrt(1.0*d*d-b1*b1);
p[i].a=a1*1.0-res;//能覆盖第i个点的最左坐标
p[i].b=a1*1.0+res;//能覆盖第i个点的最右坐标
}
}
cout<<"Case "<<ans++<<": ";
if(f){
cout<<-1<<endl;
}else{
sort(p,p+n);
int sum=1;
double r=p[0].b;
for(int i=1;i<n;i++){
if(p[i].a>r){
sum++;
r=p[i].b;
}else if(p[i].b<r){//重点,能覆盖当前的岛的右坐标要比当前取得点的右坐标要小,那么取得点的右坐标要更新成当前岛的右坐标,否则会出现无法覆盖的情况
r=p[i].b;
}
}
cout<<sum<<endl;
}
}
return 0;
}