POJ 1328 Radar Installation 贪心+sort

sort横坐标x...以岛屿为圆心.d为半径画圆...分别计算与x轴的左右交点

按照从大到小的顺序(或者从小到大都行)对x轴的交点进行判断...

#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
struct point 
{
	double x;
	double y;
}p[1010];
int cmp(point a,point b)  //结构体排序 , x升序排序 ,y 降序排序 (当x相等时,y小的点左交点显然小)
{
	if(a.x!=b.x)
		return a.x<b.x;
	else
		return fabs(a.y)>fabs(b.y);
}
double getleft(point a,double b) //获取该点与x轴的左交点
{
	return a.x-sqrt(b*b-a.y*a.y);
}
double getright(point a,double b)//获取该点与x轴的右交点
{
	return a.x+sqrt(b*b-a.y*a.y);
}
int main()
{
//	freopen("in.txt","r",stdin);
	int i,j,n;
	double m;
	int Case=0;
	while(cin>>n>>m && (n!=0 || m!=0))
	{
		Case++;
		int count=1;
		bool flag=false;
		for(i=0;i<n;i++)
		{
			cin>>p[i].x>>p[i].y;
			if(p[i].y>m)
				flag=true;
		}
		if(flag) //当y值大于半径~显然就没交点啦~输出-1
		{
			printf("Case %d: -1\n",Case);
			continue;
		}
		sort(p,p+n,cmp);
		double left = getleft(p[n-1],m);
		for(i=n-1;i>=1;i--)
		{
			if(left > getright(p[i-1],m)) //从右边开始~ 左交点和右交点比较...用left来储存满足容纳最多点的左值
			{
				count++;
				left = getleft(p[i-1],m);
			}else if(left < getleft(p[i-1],m))  //注意比较时 left的更新有坑点~这一个if没加导致我WA很多次
				left = getleft(p[i-1],m);
		}
		printf("Case %d: %d\n",Case,count);
	}
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值