Boundary
题意
给定n个二维平面上的点,问哪一个经过原点的圆的边界上包含的所给点最多
思路
三点确定一个圆(保证三个点不共线),由于题目已经确定了(0, 0)所以我们还需要枚举其余
两个点,由于(0, 0)这个点一定在圆上,所以只要我们确定了一个圆心就可以唯一确定一个
圆,使用map记录每一个圆心会对应的圆经过了多少个点,最后输出最值即可;
确定圆心的方法:使用圆的一般式(使用几何方法一直不对,可能是精度问题):
x²+y²+Dx+Ey+F=0(D²+E²-4F>0)
圆心:(-D/2, -E/2)
半径:sqrt(D^2+E^2-4*F)/2
代码
#pragma GCC optimize(2)
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define Buff ios::sync_with_stdio(false)
#define rush() int Case = 0; int T; cin >> T; while(T--)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
#define reps(i, a, b) for(int i = a; b; i ++)
#define clc(a, b) memset(a, b, sizeof(a))
#define Buff ios::sync_with_stdio(false)
#define readl(a) scanf("%lld", &a)
#define readd(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define read(a) scanf("%d", &a)
#define lowbit(n) (n&(-n))
#define pb push_back
#define lson rt<<1
#define rson rt<<1|1
#define ls lson, l, mid
#define rs rson, mid+1, r
#define y second
#define x first
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;
const int mod = 1e9+7;
const double eps = 1e-8;
const double PI = acos(-1);
const int N = 2e3+7;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
struct Point
{
double x;
double y;
Point(double x=0, double y=0):x(x), y(y) {}
};
typedef Point Vector;
double operator ^ (const Vector & v, const Vector & w) {return v.x * w.y - v.y * w.x;}
Vector operator - (const Vector & v, const Vector & w) {return Vector(v.x - w.x, v.y - w.y);}
bool operator < (const Vector & v, const Vector & w) {return v.x == w.x ? v.y < w.y : v.x < w.x;}
map<Point, int> mp;
Point p[N];
int res = 0;
void print(char c, Point t)
{
cout << c <<": "<< t.x <<" "<< t.y <<endl;
}
void calc(Point o, Point p, Point q)
{
double t1 = p.x*p.x + p.y*p.y;
double t2 = q.x*q.x + q.y*q.y;
double x = (t1*q.y-t2*p.y)/(2*(p.x*q.y-q.x*p.y));
double y = (t1*q.x-t2*p.x)/(2*(p.y*q.x-q.y*p.x));
mp[{x, y}] ++;
res = max(res, mp[{x, y}]);
}
int main()
{
int n;
cin >> n;
rep(i, 0, n-1)
{
double x, y;
cin >> x >> y;
p[i] = {x, y};
}
Point O = {0.0, 0.0};
rep(i, 0, n-2)
{
mp.clear();
rep(j, i+1, n-1)
{
Vector v = O - p[i], u = O - p[j];
if(!dcmp(v ^ u)) continue;
calc(O, p[i], p[j]);
}
}
cout << res+1 <<endl;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?