zoj 1360 || poj 1328 Radar Installation

211 篇文章 0 订阅
7 篇文章 0 订阅

贪心。纠结了好久滴说。刚发现ZOJ也有这个题,顺便A了。


开始我想的贪心算法是错的,就是如果遇到新点,就把新点作为圆上最左边的一点。。其实是不对的,因为有些情况完全可以上个圆经过右移然后覆盖掉。


所以我的算法是:以第一个点为圆的左端端点,求圆心,然后看下一个点,如果这个点没在前面那个圆内,就以当前点作为左端点做一个圆心,如果这个圆心可以覆盖之前那个圆覆盖的所有点,就相当于把这个圆向右移。

如果覆盖不完全,就以这个点位左端点重新做一个圆。


复杂度不太好计算,目测最坏是N^2。。

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define BUG puts("here!!!")
#define STOP system("pause")
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)

using namespace std;

const int MAX = 1010;
struct point{
	int x, y;
	bool operator<(const point &a) const {
		if( a.x == x )
			return y < a.y;
		return x < a.x;
	}
};
point p[MAX];
int ind[MAX];
int f[MAX];
void Rcenter(point p, double& cx, int d) {
	cx = p.x + sqrt(d*d - p.y*p.y*1.0);
}
bool notIn(point p, double cx, int d) {
	return (p.x - cx) * (p.x - cx) + p.y * p.y > d * d;
}
bool canCover(int ans, double cx, int d) {
	int i = f[ans];
	while( ind[i] == ans ) {
		if( notIn(p[i++], cx, d) )
			return false;
	}
	return true;
}
int solve(int n, int d) {
	memset(ind, 0, sizeof(ind));
	int ans = 1;
	
	sort(p, p+n);
	
	if( p[0].y > d )
		return -1;
	
	ind[0] = 1;
	f[1] = 0;
	
	double cx;
	Rcenter(p[0], cx, d);
	FOR(i, 1, n) {
		if( abs(p[i].y) > d )
			return -1;
		if( notIn(p[i], cx, d) ) {
			double t = cx; 
			Rcenter(p[i], t, d);
			if( canCover(ans, t, d) ) {
				cx = t;
				ind[i] = ans;
			} else {
				ans++;
				Rcenter(p[i], cx, d);
				ind[i] = ans;
				f[ans] = i;
			}
		} else {
			ind[i] = ans;
		}
	}
	return ans;
}

int main()
{
	int n, d;
	int ncases = 1;
	while( cin >> n >> d ) {
		if( n == 0 && d == 0 )
			break;
		
		FOR(i, 0, n)
			cin >> p[i].x >> p[i].y;
		
		cout << "Case " << ncases++ << ": " ;	
		if( d <= 0 ) {
			cout << -1 << endl;
			continue;
		}
		int ans = solve(n, d);
		cout << ans << endl;
	}

return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值