2020暑期牛客多校第二场B.Boundary(几何圆+三点共圆)

题目链接:https://ac.nowcoder.com/acm/contest/5667/B
题意:给出n个坐标点,求过原点的任意圆包含最多的坐标点个数
解题思路:
利用三点(不共线)共圆求圆心和半径
遍历讨论每两个点和原点之间构成的圆,将圆心保存下来,因为圆必过原点,所以圆心一样,则圆也一样。所以最后求相同的圆的个数ans即可
最后答案因为要求共圆的点的个数,所以为ans+1
三点共圆求圆心、半径公式:

double dis(Point x,Point y)
{
    return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
 
void solve(Point a,Point b,Point c)//三点共圆圆心公式
{
    double X=( (a.x*a.x-b.x*b.x+a.y*a.y-b.y*b.y)*(a.y-c.y)-(a.x*a.x-c.x*c.x+a.y*a.y-c.y*c.y)*(a.y-b.y) ) / (2*(a.y-c.y)*(a.x-b.x)-2*(a.y-b.y)*(a.x-c.x));
    double Y=( (a.x*a.x-b.x*b.x+a.y*a.y-b.y*b.y)*(a.x-c.x)-(a.x*a.x-c.x*c.x+a.y*a.y-c.y*c.y)*(a.x-b.x) ) / (2*(a.y-b.y)*(a.x-c.x)-2*(a.y-c.y)*(a.x-b.x));
    double R=sqrt((X-a.x)*(X-a.x)+(Y-a.y)*(Y-a.y));
    return (Point){x,y};
}
#include
#include
#include
#include
#include
using namespace std;
#define P pair
int n;
struct Point{
    double x,y;
}p[2100];
int ans;
map<P,int> m;
//double dis(Point x,Point y)
//{
//    return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
//}
 
void solve(Point a,Point b,Point c)//三点共圆圆心公式
{
    double X=( (a.x*a.x-b.x*b.x+a.y*a.y-b.y*b.y)*(a.y-c.y)-(a.x*a.x-c.x*c.x+a.y*a.y-c.y*c.y)*(a.y-b.y) ) / (2*(a.y-c.y)*(a.x-b.x)-2*(a.y-b.y)*(a.x-c.x));
    double Y=( (a.x*a.x-b.x*b.x+a.y*a.y-b.y*b.y)*(a.x-c.x)-(a.x*a.x-c.x*c.x+a.y*a.y-c.y*c.y)*(a.x-b.x) ) / (2*(a.y-b.y)*(a.x-c.x)-2*(a.y-c.y)*(a.x-b.x));
    //double R=sqrt((X-a.x)*(X-a.x)+(Y-a.y)*(Y-a.y));
    //return (Point){x,y};
    m[P(X,Y)]++;
    ans=max(m[P(X,Y)],ans);
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&p[i].x,&p[i].y);
    }
    Point origin;
    origin.x=0; origin.y=0;
    for(int i=1;i<=n;i++){
        m.clear();
        for(int j=i+1;j<=n;j++){
            if(p[i].x*p[j].y==p[i].y*p[j].x)  continue;   //共线情况
            solve(p[i],p[j],origin);
        }
    }
    cout<<ans+1<<endl;
    return 0;
}

你可能感兴趣的:(几何图形)