思路在代码的注释中
///2014.3.1 - 2014.3.2
///poj1328
/**
*对要扫描到的每个岛求出雷达可以安置在海岸上的区间[begin,end],
*对这些区间按begin从小到大排序
*从第一个区间的end点开始安置雷达,然后依次检索下一个区间,
*若下一个区间完全在刚刚安置雷达的区间内(new.begin>=pre.begin &&
* new.end<=pre.end ),则将刚刚安置的雷达重新安置在这个区间(new)的右端;
*若下一个区间的左端点在刚刚安置雷达的区间内,而右端点在刚刚安置雷达的
*区间右侧,则可忽略这个区间向下进行;
*若下一个区间与刚刚安置雷达的区间完全不相交,则在这个新区间右端内安置
*一个新雷达。
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
struct Section
{
double begin;
double end;
};
bool mycmp(Section a,Section b)
{
return a.begin < b.begin;
}
int n; ///记录区间(小岛)的数目
int d; ///记录雷达的扫描半径
Section section[1010]; ///存储每个小岛需要的雷达区间
bool init( )
{
bool legalData = true;
int x,y;
for(int i=0 ; i<n ; i++){
scanf("%d%d",&x,&y);
section[i].begin = x - sqrt(d*d-y*y);
section[i].end = x + sqrt(d*d-y*y);
if( y>d )
legalData = false;
}
std::sort(section,section+n,mycmp);
return legalData;
}
int main( )
{
// freopen("in","r",stdin);
// freopen("out","w",stdout);
int cas = 1;
while( scanf("%d%d",&n,&d)!=EOF && (n || d) ){
if( !init() ){
printf("Case %d: -1\n",cas);
cas++;
continue;
}
if( n==1 ){
printf("Case %d: 1\n",cas);
cas++;
continue;
}
double pre = section[0].end;
Section* now;
int num = 1;
for(int i=1 ; i<n ; i++){
now = &(section[i]);
if( now->begin > pre ){
num++;
pre = now->end;
}
else if( now->end < pre ){
pre = now->end;
}
}
printf("Case %d: %d\n",cas,num);
cas++;
}
return 0;
}