poj1328解题报告(贪心、线段交集)
来源:互联网 发布:网络规划设计师培训 编辑:程序博客网 时间:2024/06/05 18:05
POJ 1328,题目链接http://poj.org/problem?id=1328
题意:
有一海岸线(x轴),一半是陆地(y<0)、一半是海(y>0),海上有一些小岛(用坐标点表示P1、P2...),现要在海岸线上建雷达(覆盖半径R)。给出所有小岛的位置,和雷达半径,求最少需要多少个雷达?
思路:
1. 知道小岛位置,和雷达半径,那么以小岛为圆心,雷达覆盖半径为半径画圆,可以求出小岛与x轴有0(雷达无法覆盖)、1(雷达只能在这个点上才能覆盖)、2个交点(雷达在两点之间都能覆盖该小岛)
2. 要求最少雷达多少个,即把雷达放在1中线段的交集内。
那么这就变成了线段交集问题。(贪心)
代码:
//404k 79ms#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>typedef struct tagLINE{double left;double right;}Line;void sortLineBuf(Line *p, int num){Line temp;for (int i=0; i<num; ++i){for (int j=i+1; j<num; ++j){if (p[j].left < p[i].left){temp = p[i];p[i] = p[j];p[j] = temp;}}}}int main(){int caseNum = 0;double tempPoint;Line tempLine;while (true){int islandNum = 0, r = 0;scanf("%d%d", &islandNum, &r);if (islandNum == 0 && r == 0) break;double *p = (double*)malloc(sizeof(double) * islandNum * 2);double *pX = p;double *pY = p+islandNum;for(int i=0; i<islandNum; ++i){scanf("%lf%lf", &pX[i], &pY[i]);}//int rapar = 0;bool bImpossible = true;Line* pLine = (Line*)malloc(sizeof(Line) * islandNum);//1 转换为直线for(int i=0; i<islandNum; ++i){if (fabs(pY[i]) > r){bImpossible = false;rapar = -1;break;}tempPoint = sqrt(r*r - pY[i]*pY[i]);pLine[i].left = pX[i]-tempPoint;pLine[i].right = pX[i]+tempPoint;}if (bImpossible){rapar = 1;//2sortLineBuf(pLine, islandNum);//3 求解线段交集tempLine = pLine[0];for (int i=1; i<islandNum; ++i) { if (pLine[i].left > tempLine.right) { ++rapar; tempLine = pLine[i];} else if (pLine[i].right < tempLine.right) { tempLine = pLine[i]; } } }printf("Case %d: %d\n", ++caseNum, rapar);free(p);free(pLine);}return 0;}
0 0
- poj1328解题报告(贪心、线段交集)
- POJ1328解题报告 (贪心)
- poj1328解题报告
- POJ1328解题报告
- poj1328解题报告
- poj1328解题报告.
- POJ1328解题报告
- Radar Installation(poj1328,贪心,区间交集问题)
- poj1328 Radar Installation 解题报告
- codevs 1214 线段覆盖 贪心 解题报告
- poj1328(贪心)
- poj1328(区间贪心)
- 51node1091 线段的最长交集(贪心)
- poj1328 Radar Installation(贪心)
- POJ1328--Radar Installation(贪心)
- poj1328 Radar Installation(贪心)
- poj1328 Radar Installation(贪心)
- POJ1328-Radar Installation(贪心)
- cocos2d-x中使用json
- CCScale9Sprite的使用
- 一步步教你如何在 Visual Studio 2013 上使用 Github
- 解决TableView / ScrollView上的Menu问题(1滑出View区域还可点击2导致点击menu后View不能滑动)
- poj1753解题报告(枚举、组合数)
- poj1328解题报告(贪心、线段交集)
- OTRS的安装和配置
- poj3295解题报告(构造、算术表达式运算)
- Building a battery powered WiFi IoT Sensor with ESP8266, MS-5611 (GY-63), nodemcu and MQTT
- poj1068解题报告(模拟类)
- Struts2理解--动态方法和method属性及通配符_默认Action
- poj3080解题报告(暴力、最大公共子串)
- poj2388解题报告(排序)
- poj2299解题报告(归并排序求逆序数)