POJ 1328 Radar Installation 贪心
来源:互联网 发布:普拉达男鞋高仿淘宝店 编辑:程序博客网 时间:2024/05/01 22:00
题意:海中有许多岛屿,现在要再海岸上建立雷达,但是每个雷达的覆盖半径只有d,为了使每个岛屿都被覆盖到,求最少需要多少雷达。(建立坐标系,海岸为 x 轴,岛屿以坐标形式给出。
题解:先求出每个岛屿的被覆盖范围,即当岛屿被可以被覆盖时,雷达可以建立的最左位置及最右位置。将每个岛屿的最左位置升序排列,然后贪心求解。
题解:先求出每个岛屿的被覆盖范围,即当岛屿被可以被覆盖时,雷达可以建立的最左位置及最右位置。将每个岛屿的最左位置升序排列,然后贪心求解。
#include <cmath>#include <algorithm> #include <cstdlib>#include <iostream>using namespace std;struct p { double left; double right; } pos[1001];int cmp ( const void* a, const void* b ){p *c, *d;c = (p*)a, d = (p*)b;return c->left > d->left ? 1 : -1;}int main(){int n, res, i, test = 0;double d, current, x, y;while ( cin >> n >> d && n && d ){res = 0;for ( i = 0; i < n; i++ ){cin >> x >> y;if ( y > d )res = -1;else{pos[i].left = x - sqrt(d*d-y*y);pos[i].right = x + sqrt(d*d-y*y);}}if ( res == -1 ){cout << "Case " << ++test << ": -1" << endl;continue;}qsort ( pos, n, sizeof(p), cmp );current = pos[0].right;res = 1;for ( i = 1; i < n; i++ ){/* 当岛屿 i 的最左位置比前一个岛屿的最右位置还大时,说明前一个岛屿不可能与它后面的岛屿共享雷达,那么需要为前一个岛屿建立一个雷达 */ if ( pos[i].left > current ) {res++;current = pos[i].right;}else{/* 当岛屿 i 在前一个岛屿的被覆盖范围之内时,为了使 i 被覆盖到, current 应该改为岛屿 i 的右侧 */ if ( pos[i].right < current )current = pos[i].right;}}cout << "Case " << ++test << ": " << res << endl;}return 0;}
- POJ 1328 Radar Installation(贪心)
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 贪心
- POJ 1328 贪心 Radar Installation
- POJ 1328 Radar Installation 贪心
- POJ | 1328 Radar Installation | 贪心
- POJ 1328 Radar Installation 贪心
- 贪心-poj-1328-Radar Installation
- POJ 1328 Radar Installation (贪心)
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 贪心
- POJ 1328、Radar Installation 贪心
- poj 1328 Radar Installation(贪心)
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 贪心
- Poj 1328 Radar Installation 贪心
- POJ--1328:Radar Installation (贪心)
- wxWidgets中的常用类型转换
- 如何保证wxTextCtrl只输入数字(转)
- Android软件开发之在程序中时时获取logcat日志信息(三十三)
- wxString类与char,wchar的相互转换(转)
- Oracle面试题目
- POJ 1328 Radar Installation 贪心
- 程序员面试中遇到问题
- HDU 1251 统计难题
- 神州数码面试集合
- Linux进程间通信的各种方式及其比较
- UNIX命令大全.
- 关于px+qy类命题的研究
- QT中几种设置控件的图片背景的方法
- hdu 2196