您的位置:首页 > 其它

POJ1328 贪心

2017-05-29 13:27 393 查看
2017年3月18日 | ljfcnyali

题目大意

地图的x轴的上方为海,下方为陆地,海中有n个小岛,坐标为(isl[i].x,isl[i].y)。有一种雷达,能探测到的范围为以d为半径的圆。问海岸线上至少造多少雷达可以把所有的小岛都包含在内。注意雷达是建在海岸线上的,也就是x轴上的。

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0


Sample Output

Case 1: 2
Case 2: 1


题目分析

使用贪心,从左往右建立雷达,要尽可能多的覆盖岛屿。

AC代码

/*************************************************************************
> File Name: POJ1328.cpp
> Author: ljf-cnyali
> Mail: ljfcnyali@gmail.com
> Created Time: 2017/3/18 13:34:16
************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)

const int maxn = 1010;

struct Island {
int x, y;
} isl[maxn];

struct node {
float sta, end;
} red[maxn];

bool cmp(node x, node y) {
return x.end < y.end;
}

int main() {
int n, d, t = 0;
while(cin >> n >> d && n != 0) {
cout << "Case " << ++ t << ": ";
int max_isl = 0;
REP(i, 0, n - 1) {
scanf("%d%d", &isl[i].x, &isl[i].y);
max_isl = max(max_isl, isl[i].y);
}
if(max_isl > d || d < 0) {
cout << "-1" << endl;
continue;
}
float len;
REP(i, 0, n - 1) {
len = sqrt(1.0 * d * d - isl[i].y * isl[i].y);
red[i].sta = isl[i].x - len;
red[i].end = isl[i].x + len;
}
sort(red, red + n, cmp);
int ans = 0;
bool vis[maxn];
mem(vis);
REP(i, 0, n - 1)
if(!vis[i]) {
vis[i] = true;
REP(j, 0, n - 1)
if(!vis[j] && red[j].sta <= red[i].end)
vis[j] = true;
++ ans;
}
printf("%d\n", ans);
}
return 0;
}


本文转自:http://ljf-cnyali.cn/index.php/archives/66
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 贪心算法