[2020牛客暑期多校训练营第二场] B、Boundary

题目链接:Boundary

题意:

给你n个点,让你找最多有多少个点共圆并且该圆过原点。

题解:

首先我们知道三个点即可确定一个圆(三点不共线),可以通过任取两个线段的中垂线交点得到圆心。那么我们可以通过记录交点出现次数最多的点即为合适的圆心。
问题来了?当我们知道合适的圆心是多少时,我们如何计算符合题目要求的点的个数。
我们可以继续用"三点定圆"的原理,首先圆必过原点,这已经确定一个点了,然后我们枚举时可以先确定一个点,然后枚举其它的点。此时枚举一遍后得到的ans是其它点与确定点和原点共圆的最多的个数,然后更改确定点继续枚举其它点,最后得到的最大值就是过原点并且共圆点数最多的个数。由于确定点那一个没算,所以需+1。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pdd pair<double,double>
const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=2005;
 
struct Point
{
    double x,y;
    Point():x(),y(){};
    Point(int x,int y): x(x),y(y){}
}point[N];
 
struct Line //ax+by+c=0
{
    double a,b,c;
    Line():a(),b(),c(){}
    Line(double a,double b,double c):a(a),b(b),c(c){}
};
 
map<pdd,ll> num;
 
 
int main()
{
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
        scanf("%lf%lf",&point[i].x,&point[i].y);
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        num.clear();
        for(int j=i+1;j<n;j++)
        {
            if(fabs(point[i].x*point[j].y-point[i].y*point[j].x)>eps)
            {
                Line OA=Line(point[i].x,point[i].y,-(point[i].x*point[i].x+point[i].y*point[i].y)/2);//OA中垂线
                Line OB=Line(point[j].x,point[j].y,-(point[j].x*point[j].x+point[j].y*point[j].y)/2);//OB中垂线
                pdd p=mp((OB.c*OA.b-OA.c*OB.b)/(OA.a*OB.b-OB.a*OA.b),(OB.c*OA.a-OA.c*OB.a)/(OA.b*OB.a-OB.b*OA.a));
                num[p]++;
                ans=max(ans, num[p]);
            }
        }
    }
    printf("%lld\n",ans+1);
}

猜你喜欢

转载自blog.csdn.net/weixin_44235989/article/details/107373238