牛客第二场_B_Boundary

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;
}
/*
4
1 1
0 2
2 0
2 2
*/
posted @ 2020-11-25 14:36  youngman-f  阅读(63)  评论(0编辑  收藏  举报