您的位置:首页 > 其它

poj1328 区间贪心 <挑战程序设计竞赛>

2018-01-31 16:46 417 查看
2018-1-31

其实就是贪心,每次将所选的点尽可能的向右,那么我们所需的就会尽可能的少了。。。

#include<iostream>
#include<algorithm>
#include<cmath>
#define MIN 1e-5
using namespace std;

const int N = 1000;

struct zb{
double xz,xy;
}s[N+1];

int n,r;

//计算出可以覆盖该岛屿的发射器的坐标左右边界
void cal(int i,double p,double q){
double d=sqrt(r*r-q*q);
s[i].xz=p-d;
s[i].xy=p+d;
}

//按照右边界进行排序
bool cmp(struct zb a,struct zb b){
return a.xy<b.xy;
}

int main(){
int cnt=1;
double p,q;
while(cin>>n>>r){
if (n==0&&r==0) break;
bool flag=false;
for (int i=1;i<=n;i++){
cin>>p>>q;
if (q>r||q<0){
flag=true;
continue;
}
cal(i,p,q);
}
//所给数据不可能有解的情况,不能直接输出然后continue,而是要等待它输出完
if (flag){
cout<<"Case "<<cnt<<": -1"<<endl;
cnt++;
continue;
}
if (n==0||n==1){
cout<<"Case "<<cnt<<": "<<n<<endl;
cnt++;
continue;
}
sort(s+1,s+n+1,cmp);//排序
int res=0,i=1;
double ee=s[1].xy;
while (i<=n){
while (s[i].xz<=ee&&i<=n){
i++;
}
if (i<=n) ee=s[i].xy;
res++;
}
cout<<"Case "<<cnt<<": "<<res<<endl;
cnt++;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 贪心