习题
思路:
- 先求出能覆盖某一个岛屿的雷达可以存在的线段,问题变成了在若干条线段里,找最少的点,使得每一个线段上最少有一个点。
- 右端点排序,依次考虑每一条线段的左端点和上一次建雷达站的线段的右端点R。有交集则continue。无交集则建一个新雷达站,并更新R。
/*
* Author: Chen_zhuozhuo
* Created Time: 2020/3/17 20:49:33
* File Name: d.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
const double inf = 1e10;
const int maxn = 1005;
typedef pair<double, double> PII;
int n;
double esp = 1e-6;
double d;
///a[i].first = right, a[i].second = left;
PII a[maxn];
int main() {
cin>>n>>d;
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
if(y > d){
cout<<"-1"<<endl;
return 0;
}
double temp = sqrt(d*d - y*y);
a[i].first = x + temp;
a[i].second = x - temp;///右端点相同怎么排序
}
sort(a + 1, a + 1 + n);
int cnt = 0;
double R = -inf;
for(int i = 1; i <= n; i ++){
if(a[i].second < R + esp)
continue;
R = a[i].first;
cnt ++;
}
cout<<cnt<<endl;
return 0;
}
/*
输入样例:
3 2
1 2
-3 1
2 1
输出样例:
2
*/