您的位置:首页 > 其它

Radar Installation 贪心 hoj

2012-08-05 13:05 351 查看
/*这道贪心挺不错的.首先预处理出每个点对应的雷达极限位置.

然后就是区间覆盖的贪心揭发.先进行排序,.

开始时以第一个点的右位置为标准,当某一个点的左边大于他的时候,更新标准,ans++.

当某点的右位置小于他的时候,更新标准即可.*/

#include <stdio.h>
#include <cmath>
#include <iostream>
#include <algorithm>
struct point
{
double l,r;
} a[1010];
bool cmp(struct point a,struct point b)
{
if(a.l!=b.l)
return a.l<b.l;
}
using namespace std;
int main()
{
int n;
double d,x,y;
int cases=1;
while(scanf("%d%lf",&n,&d)==2&&(n+d))
{
bool flag=false;
for(int i=0; i<n; i++)
{
scanf("%lf%lf",&x,&y);
if(y>d) flag=true;
else
{
a[i].l=x-sqrt(d*d-y*y);
a[i].r=x+sqrt(d*d-y*y);
}
}
if(flag) printf("Case %d: -1\n",cases++);
else
{
sort(a,a+n,cmp);
int ans=1;
int t=a[0].r;
for(int i=1; i<n; i++)
{
if(a[i].l>t)
{
t=a[i].r;
ans++;
}
if(a[i].r<t)
t=a[i].r;
}
printf("Case %d: %d\n",cases++,ans);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: