牛客第二場_B_Boundary
阿新 • • 發佈:2020-11-25
題意
給定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 */