Radar Installation POJ - 1328

题意:每一个炮台的打击范围为d,现在在x轴上方有n个岛屿(可视为一个坐标),问你在x轴上至少放置几座炮台,从而保证所有岛屿都可以被攻击。如果该目标实现不了,输出-1.
思路:对于每一座岛屿,要想被某一炮台攻击,则这个炮台的安装范围可以确定(如果d < y,显然可以推出无法攻击炮台)。
通过上述操作,可以得到n个(l,r)区间,如下图所示。在这里插入图片描述
通过观察此图,我们发现当 L L L有序时,只需要研究 R R R的变化。
如果 m i n ( R 1 , R 2 , R 3 , . . . , R i ) < L i + 1 min(R_1,R_2,R_3,...,R_i) < L_{i+1} min(R1,R2,R3,...,Ri)<Li+1
------把一座炮台设置在 m i n ( R 1 , R 2 , R 3 , . . . , R i ) min(R_1,R_2,R_3,...,R_i) min(R1,R2,R3,...,Ri)处,再确定下一座炮台的位置

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1005;
typedef pair<ll, int> P;
struct node{
	double l, r;
}a[maxn];
bool cmp(node a, node b){
	return a.l < b.l;
}
int n; ll d;
int main(){
	int kase = 0;
	while(scanf("%d%lld", &n, &d), n + d){
		printf("Case %d: ", ++kase);
		bool ok = true; ll x, y;
		for(int i = 1; i <= n; i++){
			scanf("%lld%lld", &x, &y);
			if(y > d){
				ok = false;
				continue;
			}
			double eps = d * d - y * y; eps = sqrt(eps);
			a[i].l = x - eps; a[i].r = x + eps;
		}
		if(!ok){
			printf("-1\n");
		}
		else{
			sort(a + 1, a + n + 1, cmp);
			double mi = a[1].r; int ans = 1;
			for(int i = 1; i <= n; i++){
				if(a[i].l > mi){
					ans++; mi = a[i].r;
				}
				else{
					mi = min(mi, a[i].r);
				}
			}
			printf("%d\n", ans);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值