欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

计算几何 2020牛客暑期多校训练营(第二场) B Boundary

程序员文章站 2022-03-02 10:54:42
...

计算几何
题意:给n个点,一个圆边界过(0,0),求使这个圆边界过n各店中尽可能多的点,求最多过的点数。
解法:对所有点与(0,0)做中垂线,对于每条中垂线分别求与其他中垂线的交点,答案为做多交点数+1。

#include<bits/stdc++.h>
using namespace std;

const int maxn=2e3+10;

struct point{
    double x,y;
    point(){}
    point(double _x,double _y){
        this->x=_x;
        this->y=_y;
    }
    friend bool operator <(point p1,point p2){
        return p1.x==p2.x?p1.y<p2.y:p1.x<p2.x;
    }
}p[maxn];

struct line{
    point a,b;
    line(){}
    line(point _x,point _y){
        this->a=_x;
        this->b=_y;
    }
}l[maxn];

point intersection(point u1,point u2,point v1,point v2)
{
    point ret=u1;
    double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
             /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
    ret.x+=(u2.x-u1.x)*t;
    ret.y+=(u2.y-u1.y)*t;
    return ret;
}

bool eps(double x){
    return x>=-1e-8&&x<=1e-8;
}

map<point,int>mp;

int main(){
    int n;
    scanf("%d",&n);
    for (int i=1;i <=n;i++){
        scanf("%lf%lf",&p[i].x,&p[i].y);
        l[i]=line(point(p[i].x/2,p[i].y/2),point(-p[i].y+p[i].x/2,p[i].x+p[i].y/2));
    }
    int ans = 0;
    for (int i=1;i<=n; i++) {
        for (int j=i+1;j<=n;j++){
            if (eps((l[i].a.y-l[i].b.y)*(l[j].a.x-l[j].b.x)-(l[j].a.y-l[j].b.y)*(l[i].a.x-l[i].b.x))) continue;
            else {
                point x=intersection(l[i].a,l[i].b,l[j].a,l[j].b);
                mp[x]++;
                ans=max(ans,mp[x]);
            }
        }
        mp.clear();
    }
    printf("%d\n", ans + 1);
    return 0;
}

相关标签: 刷题之路