pku 1328(贪心)
2009-12-03 17:03
218 查看
大意就是给出雷达覆盖的范围和岛的坐标,要求判断最少需要多少雷达覆盖全部的岛。
首先算出每个岛到岸边的距离小于等于d的区间范围 然后根据区间排序。让尽可能多的区间重叠。
在比较的时候不断的压缩区间。没有y小于0的数据。
首先算出每个岛到岸边的距离小于等于d的区间范围 然后根据区间排序。让尽可能多的区间重叠。
在比较的时候不断的压缩区间。没有y小于0的数据。
#include <iostream> #include <algorithm> #include <cmath> using namespace std; typedef struct w { float x,y; }ext; ext test[1001]; int cmp(ext a,ext b){ return a.x<b.x; } int main(){ int n,d,a,b,num=1,ans; ext temp; bool flag; while(cin>>n>>d&&(n!=0 && d!=0)){ flag=true; ans=1; if(d<=0) flag=false; for(int i=0;i<n;++i){ cin>>a>>b; if(d*d-b*b<0) flag=false; //此判断表明d过小,雷达无论怎么样都无法覆盖 else { test[i].x=a-sqrt( float(d*d-b*b) ); test[i].y=a+sqrt( float(d*d-b*b) ); } } if(!flag) cout<<"Case "<<num++<<": "<<-1<<endl; else { sort(test,test+n,cmp); temp.x=test[0].x; temp.y=test[0].y; for(int i=0;i<n;++i){ if(test[i].x>temp.y){ //判断需要增加雷达 ++ans; temp.x=test[i].x; temp.y=test[i].y; } if(test[i].y<temp.y) temp.y=test[i].y; //压缩区间 if(test[i].x>temp.x) temp.x=test[i].x; /*cout<<temp.x<<" "<<temp.y<<endl;*/ } cout<<"Case "<<num++<<": "<<ans<<endl; } } return 0; }
相关文章推荐
- pku-1328 我的贪心
- pku 1328 Radar Installation(贪心)
- pku 1328 第一周训练 ——贪心
- Radar Installation POJ - 1328 贪心
- POJ 1328(贪心)
- poj 1328 雷达覆盖 贪心
- POJ 1328 Radar Installation(贪心)
- PKU 1065 贪心求值
- poj 1328 【Radar Installation】【贪心】
- POJ1083 Moving Tables——heap+贪心——pku1083
- poj 1328(贪心)
- pku 3253 Fence Repair(哈夫曼树,贪心)
- [ACM] POJ 1328 Radar Installation (贪心,区间选点问题)
- PKU3399贪心
- POJ 1328 Radar Installation 贪心
- POJ 1328 (贪心)
- Radar Installation(POJ 1328)(贪心)
- POJ 1328 Radar Installation(雷达安装)__贪心
- 【占坑】POJ 1328做题笔记 #贪心#
- POJ1328 贪心