将问题转化为区间重合的问题,将区间按点的x坐标排序,从左到右计算,如果当前区间跟之前计算的区间有重合部分,则把交集作为新区间,否则则表明需要另新建一个圆,把当前区间规定为新区间,并把圆个数+1.出了几处错误:1.要判断d<=0。2.坐标精度不是Int是float 3. PE错误,格式是Case+sp+:+sp+number+\n
#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
struct Point{
float x,y;
};
int n,d;
Point points[1000];
int partition(int first,int last){
int f=first;
int l=last;
Point pivot;
pivot.x = points[f].x;
pivot.y = points[f].y;
while(f<l){
while(points[l].x>=pivot.x&&f<l)
l--;
if(f<l){
points[f].x=points[l].x;
points[f].y=points[l].y;
f++;
}
while(points[f].x<pivot.x&&f<l)
f++;
if(f<l){
points[l].x=points[f].x;
points[l].y=points[f].y;
l--;
}
}
points[f].x=pivot.x;
points[f].y=pivot.y;
return f;
}
void sort(int first,int last){
if(first>=last)
return;
int pivot = partition(first,last);
if(pivot-first-1>0)
sort(first,pivot-1);
if(last-pivot-1>0)
sort(pivot+1,last);
}
int main(){
/*
streambuf *backup;
ifstream fin;
fin.open("data.in");
backup = cin.rdbuf(); // back up cin's streambuf
cin.rdbuf(fin.rdbuf()); // assign file's streambuf to cin
*/
cin>>n>>d;
int casecout=1;
while(n>0){
int flag=0;
if(d<=0){
flag=1;
}
for(int i=0;i<n;i++){
cin>>points[i].x>>points[i].y;
if(points[i].y>d)
flag=1;
}
if(flag==1){
cout<<"Case "<<casecout<<": "<<-1<<endl;
casecout++;
cin>>n>>d;
continue;
}
//sort
sort(0,n-1);
double doubled=d*d;
int count=1;
float gap=sqrt(doubled-points[0].y*points[0].y);
float left=points[0].x-gap;
float right=points[0].x+gap;
float nextleft,nextright;
for(int i=1;i<n;i++){
gap=sqrt(doubled-points[i].y*points[i].y);
nextleft=points[i].x-gap;
nextright=points[i].x+gap;
if(nextleft>right){
left=nextleft;
right=nextright;
count++;
}else{
left = left>nextleft?left:nextleft;
right = right>nextright?nextright:right;
}
}
cout<<"Case "<<casecout<<": "<<count<<endl;
casecout++;
cin>>n>>d;
}
//system("pause");
return 0;
}