题目链接: B-Boundary
Description
给出一些点,找出一个过原点的圆可以经过这些点的最多的点数
Sample Input
4
1 1
0 2
2 0
2 2
Sample Output
3
Method
- 保存输入的所有点
- 枚举任意两个点与原点组成两条线段
- 垂直平分线的交点即为经过这三个点的圆的圆心
- 利用 克莱姆法则 找出所有圆心并保存
- 找到圆心相等数量最多的圆心即为所求圆
- 利用 (n-1)*n / 2 = ans_point 求得 n
Code
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define Max 2005
#define ll long long
template <typename T> T get(T a, T b, T c, T d) { return a * d - b * c; } //求叉乘
vector< pair<int, int> > point; //存储点
vector< pair<double, double> > ctr; //存储圆心
int n, rmax=0, sum=0;
void find()
{
int x1, y1, x2, y2;
ll a11, a12, a13, a21, a22, a23, d0, d1, d2;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++) //枚举两个点
{
x1 = point[i].first; y1 = point[i].second;
x2 = point[j].first; y2 = point[j].second;
a11 = 2*x1; a12 = 2*y1; a13 = x1*x1 + y1*y1;
a21 = 2*x2; a22 = 2*y2; a23 = x2*x2 + y2*y2;
d0 = get(a11, a12, a21, a22); //求d0,如果d0为0,说明这两个点和原点在一条直线上,结束当前循环
if (d0 == 0) continue;
d1 = get(a13, a12, a23, a22);
d2 = get(a11, a13, a21, a23);
ctr.push_back(make_pair((double)d1/d0, (double)d2/d0)); //存储根据克莱姆法则计算出来的圆心点
}
}
}
int main()
{
int x, y, t=1;
point.resize(Max);
scanf("%d", &n);
for(int i=0; i<n; i++)
{
scanf("%d%d", &x, &y);
point[i] = make_pair(x, y);
}
find();
sort(ctr.begin(), ctr.end()); //将圆心点集排序
for(int i=1; i<ctr.size(); i++) //记录重复最多的圆心点的数量 rmax
{
if(ctr[i] == ctr[i-1]) t++;
else t = 1;
rmax = max(t, rmax);
}
//cout << rmax << endl;
//for(int i=0; i<n; i++)
// cout << point[i].first << " " << point[i].second << endl;
//for(int i=0; i<ctr.size(); i++)
// cout << ctr[i].first << " " << ctr[i].second << endl;
for(int i=1; i<=n; i++) //根据 ((n-1 + 1)*n)/2 = rmax 找出 n 的大小
{
if (i*i-i <= 2*rmax) sum = max(sum, i);
else break;
}
printf("%d\n", sum);
return 0;
}
蒟蒻一只,欢迎指正