[Ybtoj high-efficiency advanced 1.2] B. radar device [greedy]

Insert picture description here

analysis

Use the Pythagorean theorem to find the interval that each point can cover with d as the radius.
Pythagorean: a 2 + b 2 = c 2, l = x − sqrt (d 2 − y 2), r = x + sqrt (d 2 − y 2) a^2+b^2=c^2, l=x-sqrt(d^2-y^2), r=x+sqrt(d^2-y^2)a2+b2=c2l=xsqrt(d2Y2)r=x+sqrt(d2Y2 )
If the ordinate of this point is greater than d, directly output -1.
If the two intervals have an intersection, then one radar can be used.
Finally, find how many intervals do not intersect. If
there is no intersection, add a radar.

Upload code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;

int n,d,ans;
double x,y;

struct node
{
    
    
	double l,r;
}a[100001];

int cmp(node x,node y)
{
    
    
	return x.r<y.r;
}

int main()
{
    
    
	cin>>n>>d;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>x>>y;
		if(d<abs(y))
		{
    
    
			cout<<-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);
	ans=1;
	double jq=a[1].r;
	for(int i=2;i<=n;i++)
	{
    
    
		if(a[i].l<=jq&&jq<=a[i].r) continue;
		else
		{
    
    
			ans++;
			jq=a[i].r; 
		}
	}
	cout<<ans;
	return 0;
}

Guess you like

Origin blog.csdn.net/dglyr/article/details/112132167