您的位置:首页 > 其它

pku 1328(贪心)

2009-12-03 17:03 218 查看
大意就是给出雷达覆盖的范围和岛的坐标,要求判断最少需要多少雷达覆盖全部的岛。

首先算出每个岛到岸边的距离小于等于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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: