D e s c r i p t i o n Description Description
有 n n n个建筑物,第 i i i个建筑物在笛卡尔坐标系上的坐标为 ( x i , y i ) (x_i,y_i) (xi,yi),你需要在 x x x轴上安装一些雷达,每个雷达的侦察半径均为 d d d,要求每个建筑物都至少被一个雷达侦测到,求最少要安装几个雷达。
I n p u t Input Input
第一行两个正整数 n , d n,d n,d。
接下来 n n n行,第 i i i行两个整数 x i , y i x_i,y_i xi,yi。
O u t p u t Output Output
输出一行表示答案,若没有解决方案,则答案为 − 1 -1 −1。
S a m p l e Sample Sample I n p u t Input Input
3 2
1 2
-3 1
2 1
S a m p l e Sample Sample O u t p u t Output Output
2
H i n t Hint Hint
对于 100 % 100\% 100%的数据,有 1 ≤ n ≤ 1 0 3 1\leq n\leq 10^3 1≤n≤103 。
T r a i n Train Train o f of of T h o u g h t Thought Thought
贪心
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define dou double
using namespace std;
struct wh_
{
dou x, y;
}A[1250];
int x, y, n, m, Ans = 1;
bool nm(wh_ i, wh_ j)
{
return i.y < j.y;}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i)
{
scanf("%d%d", &x, &y);
if(abs(y) > m)
{
printf("-1\n");return 0;}
A[i].x = (dou)x - (dou)sqrt(m * m - y * y);
A[i].y = (dou)x + (dou)sqrt(m * m - y * y);
}
sort(A + 1, A + n + 1, nm);
dou k = A[1].y;
for(int i = 2; i <= n; ++i)
if(k < A[i].x){
Ans++, k = A[i].y;}
printf("%d", Ans);
return 0;
}