POJ 1328【Radar Installation】

13 篇文章 0 订阅
8 篇文章 0 订阅

题目链接

思路:

  • 参见AC_hell的Blog
  • 贪心。如何贪心?我们首先把每个雷达换成点,把岛屿换成雷达,让岛屿reach out for雷达。
  • 雷达建在海岸线即x轴上,显然雷达应建在一些重叠的区域内。如何选取?
  • 把每个岛屿形成的等大的圆与x轴的两个交点记录为一个区间,只要有一个雷达建在这个区间里,那么这个岛屿就被照顾到。所以我们将所有的区间的L从左到右排序,遍历所有的区间,若有重叠则 ans 不变,若没有则 ans+1 注意每次都要更新区间
  • 不知道哪里错了。

WA代码:

#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>

using namespace std;

struct SEC//Section
{
	double l,r;
	SEC(double a=0,double b=0){l = a,r = b;}
	friend bool operator > (const SEC &a,const SEC &b)
	{
		a.l > b.l;
	}
};
priority_queue<SEC , vector<SEC> , greater<SEC> > Q;
int n,d;
bool Imp;
int ans;
int cas = 0;

void init(){
	while(!Q.empty())
		Q.pop();
	ans = 0;
	Imp = false;
	return ;
}

void AC(){
	while(!Q.empty()){
		SEC cur = Q.top();
		Q.pop();
		double l = cur.l;
		double r = cur.r;
		while(!Q.empty()){
			cur = Q.top();
			if(cur.l < r){
				l = cur.l;//attention
				r = min(r , cur.r);//attention
				Q.pop();//attention
			}
			else
				break;
		}
		ans++;
	}
	return ;
}

int main(){
	while(true){
		init();
		cas++;
		cin>>n>>d;
		if(n + d == 0)
			break;
		for(int i=1;i<=n;i++){
			int x,y;
			cin>>x>>y;
			if(y > d)
				Imp = true;
			else
				Q.push((SEC)(x-sqrt(d*d - y*y) , x+sqrt(d*d - y*y)));
		}
		if(Imp){
			cout<<"Case "<<cas<<": "<<-1<<endl;
			continue;
		}
		AC();
		cout<<"Case "<<cas<<": "<<ans<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值