112.雷达设备
两个注意的地方:
- 按照 尾 排序
- 以选中的第一个区间的 尾 去和下一个区间的 首 比较
#include<bits/stdc++.h>
using namespace std;
typedef pair<double,double>PDD;
const int N=1010;
const double eps=1e-6, INF=1e10;
int n,d;
PDD seg[N];
int main(){
cin>>n>>d;
bool success=true;
for(int i=0;i<n;i++){
int x,y;
cin>>x>>y;
if(y>d){
success =false;
break;
}
double len=sqrt(d*d-y*y);
seg[i]={x+len,x-len};//尾比较
}
if(!success) puts("-1");
else{
sort(seg,seg+n);//从小到大
int res=0;
double last=-INF;
for(int i=0;i<n;i++){
if(seg[i].second>last+eps){
res++;
last=seg[i].first;
}
}
cout<<res<<endl;
}
return 0;
}