poj 1328Radar Installation(uvaoj 2519)区间选点

在坐标系中,用x轴一下表示陆地,以上表示海洋,海洋中有若干小岛,给出小岛的坐标,要在海岸边安装雷达,覆盖到小岛,雷达的覆盖半径是d,总共有n个小岛。
求出覆盖所有小岛的最少雷达数。如果小岛的y坐标大于d,则雷达就覆盖不了这个小岛,不存在解。否则一定有解。
对于每个小岛,覆盖它的雷达安装位置有一个范围,我们可以计算出这个范围。就是以小岛为圆心,以d为半径画一个圆,与x轴的两个交点就是这个范围。
则这个问题就变成了,在n个 范围内选尽量少的点,使得每个区间内至少有一个点。将第i个区间记为(li,ri)。现将所有区间以ri递增排序,ri相同按li降序排列。
则对于当前区间,选哪一个点最好呢,答案是选择最右边的点即ri。可以反证选ri比不选ri优。这样扫描n个区间就可以得到答案了。
代码如下:
/*************************************************************************
	> File Name: 2519.cpp
	> Author: gwq
	> Mail: gwq5210@qq.com 
	> Created Time: 2014年10月10日 星期五 17时18分58秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define SQR(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
#define middle(x, y) ((x + y) >> 1)

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

typedef struct Node {
	double l, r;
}Node;

#define N 1010

int n;
double dis;
Node node[N];

//区间选点
bool operator <(Node u, Node v)
{
	if (fabs(u.r - v.r) < esp) {
		return u.l > v.l;
	} else {
		return u.r < v.r;
	}
}

int main(int argc, char *argv[])
{
	int c = 0;
	while (scanf("%d%lf", &n, &dis) != EOF) {
		if (n == 0) {
			break;
		}

		int flag = 1;
		//将每个小岛转换成区间
		for (int i = 0; i < n; ++i) {
			scanf("%lf%lf", &node[i].l, &node[i].r);
			if (node[i].r > dis) {
				flag = 0;
			} else {
				double tmp = sqrt(dis * dis - node[i].r * node[i].r);
				node[i].r = node[i].l + tmp;
				node[i].l = node[i].l - tmp;
			}
		}

		if (flag == 0) {
			printf("Case %d: -1\n", ++c);
		} else {
			sort(node, node + n);

			int ans = 1;
			double pos = node[0].r;
			for (int i = 1; i < n; ++i) {
				if (node[i].l > pos) {
					++ans;
					pos = node[i].r;
				}
			}

			printf("Case %d: %d\n", ++c, ans);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值