雷达设备

给定n个线段 求最少设立多少个点 能够让每个线段上都有一个点

在左端点建立,维护右边界

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;
const int N=500010;
const int INF=0x3f3f3f3f;
int read()
{
    int x=0,f=0,c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return f?-x:x; 
}

struct Node
{
	double l,r;
}p[N];
bool cmp(Node a,Node b)
{
	return a.l<b.l;
}
int n,d;

int main()
{
	n=read(); d=read();
	for(int i=1;i<=n;i++)
	{
		int x=read(),y=read();
		double tmp=d*d-y*y;
		if(tmp<0) {puts("-1"); return 0;}
		p[i]=(Node){ x-sqrt(tmp),x+sqrt(tmp) };
	}
	sort(p+1,p+n+1,cmp);
	int ans=0;
	double pos,tr=-INF;
	for(int i=1;i<=n;i++)
	{
		if(p[i].l>tr) ans++,tr=p[i].r;
		else tr=min(tr,p[i].r);
	}
	printf("%d",ans);
}

贪心类型: 决策包容性

在前公共区间建立雷达站包括了在后一个区间新建雷达站的情况

证明技巧: 对于每个区间进行满足条件的讨论

posted @ 2021-12-30 16:34  __iostream  阅读(36)  评论(0)    收藏  举报