poj 1328 贪心 水题

这道题目让我明白到: 求圆心在x轴上,d为半径作圆覆盖已知点,可以先过已知点圆圆心,以d为半径与x轴交的两点,改区间变为所求圆的圆心范围;


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1005;
double d;
int n;

struct Point
{
	double x,y;
	Point() {}
	Point(double xx,double yy):x(xx),y(yy) {}
	void read()
	{
		scanf("%lf%lf",&x,&y);
	}
}p[maxn];

struct ext
{
	double l,r;
	bool operator < (const ext& a) const
	{
		return (l<a.l);
	}
}e[maxn];

double dis(double y)
{
	return sqrt(d*d-y*y);
}

double sqr(Point a,Point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int main()
{
	int kase=1;
	while(1)
	{
		scanf("%d%lf",&n,&d);
		if(n==0 && d==0) break;

		bool flg=true;
		for(int i=0;i<n;i++)
		{
			p[i].read();
			if(p[i].y>d)
				flg=false;
			else
			{
				double x=dis(p[i].y);
				e[i].l=p[i].x-x;
				e[i].r=p[i].x+x;
			}
		}

		if(!flg)
			printf("Case %d: %d\n",kase++,-1);
		else
		{
			int ans=1;
			sort(e,e+n);
			double R=e[0].r;
			int i=1;
			while(i<n)
			{
				if(e[i].l>R)
				{
					R=e[i].r;
					ans++;
				}
				else if(e[i].r<R)
				{
					R=e[i].r;
				}
				i++;
			}
			
			printf("Case %d: %d\n",kase++,ans);
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值