链接:https://ac.nowcoder.com/acm/contest/5667/B
来源:牛客网
题目描述:
Given n points in 2D plane. Considering all circles that the origin point (0,0) is on their boundries, find the one with the maximum given points on its boundry. Print the maximum number of points.
输入描述:
The first line contains one integer n (1≤n≤2000), denoting the number of given points.
Following {n}n lines each contains two integers x, y(∣x∣,∣y∣≤10000), denoting a given point (x,y).
It's guaranteed that the points are pairwise different and no given point is the origin point.
输出描述:
Only one line containing one integer, denoting the answer.
solution:
题意:给n个二维坐标上的点,问过原点的圆上最多能有几个点在该圆上
有篇博客讲了关于三点确定一个圆的详细内容:http://www.skcircle.com/?id=642&tdsourcetag=s_pctim_aiomsg
然后就基本上是套板子了,不过要注意精度问题,因此尽量减少除法操作
#include<bits/stdc++.h> using namespace std; struct point{ double x,y; }p[2005]; int n; typedef long long ll; typedef pair<double,double>P; map<P,int>mp; int main() { cin>>n; int res=0; for(int i=0;i<n;i++)cin>>p[i].x>>p[i].y; for(int i=0;i<n;i++) { mp.clear(); for(int j=i+1;j<n;j++) { double a=(p[i].x-0)*2; double b=(p[i].y-0)*2; double c=(p[i].x-p[j].x)*2; double d=(p[i].y-p[j].y)*2; double e=(p[i].x*p[i].x-0)+(p[i].y*p[i].y-0); double f=(p[i].x*p[i].x-p[j].x*p[j].x)+(p[i].y*p[i].y-p[j].y*p[j].y); double det=b*c-a*d; if(det==0)continue; double x0=-(d*e-b*f)/det; double y0=-(a*f-c*e)/det; mp[P(x0,y0)]++; res=max(res,mp[P(x0,y0)]); } } cout<<res+1; }