Radar device
Problem solving ideas
For each building (x, y),
we can calculate the radar construction interval [l,r] that can detect the object on the x-axis, which is obtained
by the Pythagorean theorem :
l=x- d 2 − y 2 2 \ sqrt[2]{d^2-y^2}2d2−Y2 r=x+ d 2 − y 2 2 \sqrt[2]{d^2-y^2} 2d2−Y2
When d 2 -y 2 <0 , that is, d <y when
the building will not likely be the radar to the investigation
we can all buildings are converted to build the radar range
so that the problem would become
a given interval of n
seek at least a few Points can cover all intervals
Greedy strategy:
1. Press the right end of row increasing order
2. If the current range contains a selection of the last point, you skip
or put a new point at the right end point of the interval
AC code
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,d,x,y,s=1;
double tail;
struct node
{
double l,r;
}a[1005];
bool cmp(node x,node y)
{
return x.r<y.r;
}
int main()
{
scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++)//将建筑物转区间
{
scanf("%d%d",&x,&y);
if(d<y){
printf("-1");return 0;}//特判
a[i].l=x-sqrt(d*d-y*y);
a[i].r=x+sqrt(d*d-y*y);
}
sort(a+1,a+n+1,cmp);//排序
tail=a[1].r;//初值
for(int i=2;i<=n;i++)//贪心
if(a[i].l>tail){
s++;tail=a[i].r;}
printf("%d",s);
return 0;
}