POJ1328 贪心
2017-05-29 13:27
393 查看
2017年3月18日 | ljfcnyali
题目大意
地图的x轴的上方为海,下方为陆地,海中有n个小岛,坐标为(isl[i].x,isl[i].y)。有一种雷达,能探测到的范围为以d为半径的圆。问海岸线上至少造多少雷达可以把所有的小岛都包含在内。注意雷达是建在海岸线上的,也就是x轴上的。
Sample Input
Sample Output
题目分析
使用贪心,从左往右建立雷达,要尽可能多的覆盖岛屿。
AC代码
本文转自:http://ljf-cnyali.cn/index.php/archives/66
题目大意
地图的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 1328 安装雷达 - (贪心)
- poj 1328 区间,贪心
- POJ 1328:Radar Installation(贪心)
- POJ1328 Radar Installation(贪心)
- POJ 1328 Radar Installation(区间贪心)
- POJ - 1328 Radar Installation 贪心
- Radar Installation(POJ 1328 贪心)
- poj1328(贪心)
- POJ 1328 Radar Installation 贪心
- Radar Installation - POJ 1328 贪心
- Radar Installation POJ - 1328 贪心
- poj 1328 Radar Installation(nyoj 287 Radar):贪心
- poj 1328 贪心
- poj 1328 Radar Installation 【贪心】
- POJ 1328 && NYOJ 891 - 贪心 区间选点问题
- POJ 1328 Radar Installation(贪心)
- 【贪心】[POJ1328]Radar Installation
- POJ 1328 Radar Installation (贪心)
- poj 1328 Radar Installation(贪心)
- POJ_1328 Radar Installation(贪心)