112. Radar equipment [greedy]

insert image description here
insert image description here
Then the problem becomes the interval merge problem. Give us n and give the interval. Combine the intersecting intervals to find the number of disjoint intervals.

#include<bits/stdc++.h>
using namespace std;
typedef pair<double,double> pdd;
int n,r,flag;
vector<pdd>ve; 
int main(void)
{
    
    
    cin>>n>>r;
    for(int i=0;i<n;i++)
    {
    
    
        double x,y; cin>>x>>y;
        if(y>r) flag=1;
        double len=sqrt(r*r-y*y);
        ve.push_back({
    
    x+len,x-len});
    }
    double last=-1e9;
    int cnt=0;
    sort(ve.begin(),ve.end());
    for(int i=0;i<ve.size();i++)
        if(ve[i].second>last) last=ve[i].first,cnt++;
    if(flag) puts("-1");
    else cout<<cnt;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_46527915/article/details/123625417