POJ1328——Radar Installation(贪心)
来源:互联网 发布:wps word表格数据计算 编辑:程序博客网 时间:2024/05/16 19:41
题目链接
题目大意是在给定的坐标系中在x轴上方有许多岛屿,然后你可以在x轴上设置雷达,雷达有探测范围d,要求尽量少的雷达将所有岛屿探测入内,如果无法搜索完全部岛屿,输出-1。先开始贪心方式错了,送了两个wrong,起先认为按横坐标从小到大排序然后更新雷达位置就行了,后来发现雷达的位置并不是随岛屿横坐标越大,就一定会更新到越大的位置,有可能会变小,所以这种想法是不对的,后来换成从雷达覆盖区间的交集来考虑才得出正确方式,还是先排序,对当前岛屿所能设置的雷达区间与先前存在的雷达区间取交集,如果有交集,证明先前设置的雷达区间交集部分仍旧能探测当前岛屿,那么久直接更新雷达区间为交集区间。如果取不到交集,那么久从新设置新的雷达,并将新的区间更新为雷达区间,每做一次这样的更新cont++即可,最后cont值即为最少所需雷达数。
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;struct node{ double x; double y; node() { x=0; y=0; }} s[1005];bool cmp(node a,node b){ if(a.x!=b.x) return a.x<b.x; else return a.y<b.y;}int main(){ //freopen("in.in","r",stdin); int n; double d; int time=0; while(scanf("%d%lf",&n,&d)) { if(n==0&&d==0)break; time++; for(int i=0;i<n;i++) scanf("%lf %lf",&s[i].x,&s[i].y); sort(s,s+n,cmp); double pre_l=0, pre_r=0; int cont=0; bool flag=0; for(int i=0;i<n;i++) { if(d<s[i].y) { flag=1; break; } double now_l=s[i].x-sqrt(d*d-s[i].y*s[i].y); double now_r=s[i].x+sqrt(d*d-s[i].y*s[i].y); if(i==0) { pre_l=now_l; pre_r=now_r; cont++; continue; } if(pre_r<now_l)//无交集 { pre_l=now_l; pre_r=now_r; cont++; }else//有交集,更新交集 { if(pre_l<=now_l)pre_l=now_l; if(now_r<=pre_r)pre_r=now_r; } } if(flag)printf("Case %d: -1\n",time); else printf("Case %d: %d\n",time,cont); } 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(贪心)
- POJ1328 Radar Installation(贪心)
- 【贪心】[POJ1328]Radar Installation
- POJ1328-Radar Installation(贪心)
- 对路径"......"的访问被拒绝--解决办法
- git常用不太熟指令
- 传统密码技术
- 安卓横竖屏切换问题总结
- linux打乱以及纠正文件顺序指令
- POJ1328——Radar Installation(贪心)
- python re模块详解
- MySQL 手动清除binlog
- Android事件交互
- Android电源管理-休眠简要分析
- opentld
- 数据结构——图的简介及概念
- httpd 启动 时分别启动了 80 端口和8080 端口
- nrlsensorsim-2.27 installation