1. 程式人生 > 實用技巧 >2020牛客暑期多校訓練營(第二場)B三點確定圓心

2020牛客暑期多校訓練營(第二場)B三點確定圓心

題意:給定原點及n個點,找到一個圓使得儘可能多的點在圓上
題解:三點可以確定一個圓,原點固定,遍歷兩個點去確定圓心,並用map儲存圓心,當再次得到一個相同的圓心時,map++(圓心相同,且有共點必定為同一個圓)

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
using namespace std;
const int maxn = 2e3+10;
const double eps=1e-5;
struct Point{
    double x,y;
}a[maxn];
map
<pair<double,double>,int> mp; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ scanf("%lf %lf",&a[i].x,&a[i].y); } int ans = 0; for(int i=1;i<=n;i++){ mp.clear(); for(int j=i+1;j<=n;j++){ double x1 = a[i].x,x2 = a[j].x,y1 = a[i].y,y2 = a[j].y;
double x3=0,y3=0; double a = x1-x2; double b = y1-y2; double c = x1-x3; double d = y1-y3; double e = ((x1*x1-x2*x2)+(y1*y1-y2*y2))/2.0; double f = ((x1*x1-x3*x3)+(y1*y1-y3*y3))/2.0; double det = b*c-a*d; if(fabs(det)<eps){
continue; } double x = -(d*e-b*f)/det; double y = -(a*f-c*e)/det; ans = max(ans,++mp[{x,y}]); } } cout<<ans+1<<endl; return 0; }