(Relax 1.6)POJ 1328 Radar Installation(利用数据有序化进行贪心选择)

本文提供了一道来自POJ平台的题目解答思路与代码实现。该题涉及通过输入圆的直径和坐标来计算能够覆盖所有圆的最小数量的区间。文章包含完整的C++代码示例,并介绍了关键算法步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * POJ_1328.cpp
 *
 *  Created on: 2013年11月18日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 1010;

struct tt{
	double l,r;
}p[maxn];

int n,d;

bool flag;
void init(){
	flag = true;

	int i;
	double x,y;

	for(i = 1 ; i <= n ; ++i){
		scanf("%lf%lf",&x,&y);

		if(d < y){
			flag = false;//***改成这种写法....不要使用return.因为您将读数据的操作也放在这里了,而测试数据已不满足就return的话,可能会导致数据没有读完而报RE
		}

		double h = sqrt(d*d - y*y);
		p[i].l = x - h;
		p[i].r = x + h;
	}
}


bool cmp(tt a, tt b){
	if( b.r - a.r > 10e-7){
		return true;
	}

	if(abs(a.r - b.r) < 10e-7 && ( b.l - a.l > 10e-7)){
		return true;
	}

	return false;
}


void work(){
	if( d == -1){
		printf("-1\n");
		return ;
	}

	sort(p+1,p+1+n,cmp);

	int ans = 0;
	double last = -10000.0;
	int i;
	for(i = 1 ; i <= n ; ++i){
		if(p[i].l <= last){
			if(p[i].r <= last){//***这个一定要有,用于处理(-1,5)、(1,3)这种情况
				last = p[i].r;
			}
			continue;
		}

		ans++;
		last = p[i].r;
	}

	printf("%d\n",ans);
}


int main(){
	int counter = 1;
	while(scanf("%d%d",&n,&d)!=EOF,n||d){
		printf("Case %d: ",counter++);
		init();

		if(!flag){
			printf("-1\n");
		}else{
			work();
		}
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值