Poj1328Radar Installation雷达安装

原题链接

经典贪心,转化为问题为,对于所有的区间,求最小的点数能使每个区间都至少有一个点。

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

const int MAXN=100000+5;
const double eps=0.000001;
struct seg
{
	double l,r;
	bool operator < (const seg& b)const
	{
		if(l==b.l)
			return r<b.r;
		return l<b.l;
	}
}e[MAXN];
int cs;

int main()
{
	int n,R;
	while(~scanf("%d%d",&n,&R))
	{
		if(n==0&&R==0)
			 break;
		int x,y;
		bool f=0;
		memset(e,0,sizeof e);
		for(int i=1;i<=n;++i)
		{
			scanf("%d%d",&x,&y);
			if(y==R)
			{
				e[i]=(seg){1.0*x,1.0*x};
			}
			else if(y<R)
			{
				double t=sqrt(R*R-y*y);
				e[i]=(seg){x-t,x+t};
			}
			else
				f=1;
		}
		int ans;
		if(f)
			ans=-1;
		else
		{
			sort(e+1,e+n+1);
			ans=1;
			double t=e[1].r;
			for(int i=2;i<=n;++i)
			{
				if(e[i].l>t)
				{
					++ans;
					t=e[i].r;
				}
				if(e[i].r<t)
					t=e[i].r;
			}
		}
		printf("Case %d: %d
",++cs,ans);
	}
	return 0;
}