POJ 1328 Radar Installation 贪心算法
来源:互联网 发布:树达学院教务网络系统 编辑:程序博客网 时间:2024/06/09 01:52
题目:http://poj.org/problem?id=1328
题目大意:在一个无线长的直线的海岸线上,海岸线上边是岛屿,在海岸线上放雷达,雷达的辐射范围是D,问至少放几个雷达可以覆盖所有的塔。
分析:如下图所示,针对一个点,她的雷达的位置范围如下,因此我们按照x对所有的岛屿点进行排列,如果每个岛屿的圆心有重合,那么他们可以用一个雷达所覆盖,如果不能我们需要重新设置一个新的雷达,雷达的位置范围为新的点的范围。
代码如下:
#include<stdio.h>#include <math.h>#define ONLINE#define max(a,b) (a)>(b)?(a):(b)#define min(a,b) (a)<(b)?(a):(b)void online(){#ifdef ONLINE#elsefreopen("1328.in","r", stdin);freopen("1328.out","w", stdout);#endif}int n, d;int cas = 1;const int SIZE = 1001;int point[SIZE][2];double range[SIZE][2];int result;int calrange(){//排序for (int i=0; i<n; i ++){for(int j=i+1; j < n; j ++){if (point[i][0] > point[j][0]){int x = point[i][0];int y = point[i][1];point[i][0] = point[j][0];point[i][1] = point[j][1];point[j][0] = x;point[j][1] = y;}//end if}//end for j}//end for ifor (int i=0; i < n; i ++){if(point[i][1] > d)return -1;double r = sqrt((double)(d * d - point[i][1] * point[i][1]));range[i][0] = point[i][0] - r;range[i][1] = point[i][0] + r;}}void calresult(){double x = range[0][0];double y = range[0][1];result = 1;for (int i = 1; i < n; i ++){if (range[i][0]<= y && range[i][1] >= x){x = max(x, range[i][0]);y = min(y, range[i][1]);}else{result ++;x = range[i][0];y = range[i][1];}//end else}//end for}void read(){while (scanf("%d%d", &n, &d)!=EOF && n != 0 && d != 0){for(int i=0; i < n; i ++)scanf("%d%d", &point[i][0], &point[i][1]);result = 0;if (calrange() == -1){printf("Case %d: %d\n", cas, -1);}else{calresult();printf("Case %d: %d\n", cas, result);}cas ++;}//end while}int main(){online();read();return 0;}运行结果如下:1328Accepted200K32MSC++1663B2011-07-30 16:07:54
- 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 贪心
- 2011.07.30
- java开发工程师和网站优化师?我该选择哪条路呢
- POJ 2503 Babelfish hash / qsort+ bsearch
- 帧差发去除背景类肤色影响的肤色检测法
- 纠结的二分。。。
- POJ 1328 Radar Installation 贪心算法
- 如何确定web里面的数据分页
- PHP的文件上传功能
- T-SQL 变量作用域
- pb10以上版本调用ANSI编码的api会出现乱码的解决方法
- zoj1514
- 程序员越老越优秀吗?
- 如何在MySql中记录SQL日志(例如Sql Server Profiler)
- VIM中括号的自动补全与删除