Catching Fish(HDU-1077)


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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值