poj1328 Radar Installation 贪心
来源:互联网 发布:淘宝信用卡付款手续费 编辑:程序博客网 时间:2024/05/17 07:16
传送门:poj1328 Radar Installation
题目大意
在一条水平的海岸线上,上面是海洋下面是大陆,还想里面有n个孤岛,在海岸线上可以放雷达,雷达的最大距离为d,题目原图:
可以抽象为一个直角坐标系,p1(1,2),p2(-3,1),p3(2,1)分别为三个孤岛,在海岸线上最多放两个雷达就可以检测到所有的孤岛, 输入
:第一行输入孤岛数量n,以及雷达检测的最远距离d,
下面的n行分别为孤岛在坐标系中的坐标,当输入(0,0)的时候结束程序。 输出
: 对于每一组数据,输出需要多少个雷达,如果有不能检测到的输出-1;
按照下面得格式输出:Case X: Y,就算是输出-1的话也要输出前面的case
解题思路
我们可以以每个孤岛在直角坐标系中的坐标为圆心,雷达的最大检测距离为半径做一个圆,找出这个圆和海岸线(y=0)的交点区间,如下图:
孤岛与海岸线的交点分别为(x1,0),(x2,0),也就是说海岸线在[x1,x2]之间都能检测到该孤岛。其他的孤岛一样的道理。然后我们按照这个区间的左区间进行排序。
然后分为下面的情况进行判断:
我们设排序完的区间的第一个的右区间为最长区间
1. 当第i个的做区间大于最长区间的时候,雷达数+1;
2. 当第i个区间的左区间小于最长右区间,并且第i个区间的右区间大于最长区间,最长区间不变
3. 当第i个区间的左区间小于最长右区间,并且第i个区间的右区间小于最长区间,最长区间变为第i个区间的右区间。
AC代码
#include<cstdio>#include<cstring>#include<set>#include<stack>#include<cmath>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<cstdlib>#include<iostream>using namespace std;typedef long long LL; //lldstruct Node{ double left; double right;}radar[100000];double min(double a,double b){ if(a>=b) return b; else return a;}double max(double a,double b){ if(a>=b) return a; else return b;}bool cmp(const Node a,const Node b){ return a.left < b.left;}int main(){ int n,d; int islandLeft,islandRight; int i,j; int count1=0; while(true) { count1++; bool flag = false; //当超过雷达的检测范围的时候,改变状态 int sum=1; scanf("%d%d",&n,&d); if(n==0&&d==0) break; for(i=0;i<n;i++) { scanf("%d%d",&islandLeft,&islandRight); if(islandRight > d) flag = true; radar[i].left = islandLeft * 1.0 - sqrt(d * d - islandRight * islandRight); ////按照圆的方程求出左区间和下面的额右区间 radar[i].right= islandLeft * 1.0 + sqrt(d * d - islandRight * islandRight); } if(flag) printf("Case %d: -1\n",count1); else { sort(radar,radar+n,cmp); double maxRight = radar[0].right; for(i=1;i<n;i++) { if( (radar[i].left - maxRight) >0.0000001)//浮点数不能直接比较 { maxRight = radar[i].right; sum++; } else if( (radar[i].right - maxRight) <0.0000001) { maxRight = radar[i].right; } } printf("Case %d: %d\n",count1,sum); } } return 0;}
0 0
- POJ1328 Radar Installation 贪心
- POJ1328 Radar Installation 贪心
- 【贪心】POJ1328 Radar Installation
- poj1328 Radar Installation 贪心
- 【POJ1328】Radar Installation 贪心
- POJ1328 Radar Installation 贪心
- 贪心- poj1328 Radar Installation
- POJ1328 Radar Installation 贪心
- poj1328 Radar Installation 贪心
- poj1328 Radar Installation 贪心
- POJ1328 Radar Installation (贪心)
- POJ1328 Radar Installation(贪心)
- POJ1328 Radar Installation(贪心)
- 【贪心】[POJ1328]Radar Installation
- POJ1328-Radar Installation(贪心)
- ZOJ1360 POJ1328 Radar Installation, 贪心
- poj1328 Radar Installation(贪心)
- POJ1328--Radar Installation(贪心)
- 【2001】关于N!的问题
- 三、观察者模式
- 欢迎使用CSDN-markdown编辑器
- 06、[]的使用
- VMware虚拟机中显示CPU不支持VT的解决方法
- poj1328 Radar Installation 贪心
- smarty入门教程
- TheDAO被攻击事件考察报告
- 为什么用copy for NSString属性?
- Java NIO 学习(一)--简介
- java组合模式(Composite)-结构性
- div反弹
- Unity-【编辑器扩展】一键批量修改预设UGUI Text字体
- java判断字符串是否为空的方法总结