1 题意
给定一个半径为
R
R
R的圆,要求用这个圆尽可能多地覆盖平面上的点,要求最大覆盖点数。
链接:link。
2 思路
命题:保证两个点在圆周上,一定能找到最优解。
证明:对于任意一个最优解,如果其边界少于两个点,则一定可以通过移动圆,使得至少让两个点在圆上,而不是最优性。
根据以上命题,可以枚举任意两个点在圆上,然后统计有多少个点在圆内。
2.1 时间复杂度分析
每个任意两个点,之后还有判断其他点是否在园内,所有时间复杂度为 O ( n 3 ) \mathcal{O}(n^{3}) O(n3)。
2.2 实现
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
typedef pair<double,double> point;
const double eps=1e-8;
double dis(point& a,point& b){
return sqrt(pow(a.first-b.first,2)+pow(a.second-b.second,2));
}
point center(point p1,point p2){
point mid=point{(p1.first+p2.first)/2,(p1.second+p2.second)/2};
double angle=atan2(p2.first-p1.first,p1.second-p2.second);//计算的是p1p2的辐射角,再逆旋90度
double d=sqrt(1-pow(dis(p1,p2),2)/4); //也就是垂直平分线的一个方向的方向角
return point{mid.first+d*cos(angle),mid.second+d*sin(angle)};
}
int main(){
int T;cin>>T;
while(T--){
vector<point> points;
int n;cin>>n;
for(int i=0;i<n;i++){
point tmp;
cin>>tmp.first>>tmp.second;
points.push_back(tmp);
}
int maxx=1;
for(int i=0;i<points.size();i++){
for(int j=0;j<points.size();j++){
if(i==j||dis(points[i],points[j])/2>1) continue;
int cnt=0;
point cen=center(points[i],points[j]);
for(int k=0;k<points.size();k++){
if(dis(cen,points[k])<1+eps) cnt++;
}
maxx=max(maxx,cnt);
}
}
cout<<maxx<<endl;
}
return 0;
}