1. 程式人生 > 其它 >繫結方法和非繫結方法

繫結方法和非繫結方法

給定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);
}

貪心型別: 決策包容性

在前公共區間建立雷達站包括了在後一個區間新建雷達站的情況

證明技巧: 對於每個區間進行滿足條件的討論