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

来源:互联网 发布:ubuntu 本地源 编辑:程序博客网 时间:2024/05/22 12:21
poj 1328Radar Installation
在坐标系中,用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 1010int 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;}


0 0
原创粉丝点击