1. 程式人生 > 實用技巧 >2020牛客多校補題 第二場

2020牛客多校補題 第二場

B.Boundary

給定一個二維平面,以及n個點,求一個過圓心的圓,問最多有多少個點在圓上。

思路:

列舉C (n,2) 個點 ,從而得到圓心。若在在一個圓上,必然圓心也是同一個,圓心同一個也必在圓上。

要注意,列舉一個迴圈就計算一次,否則兩次再算要考慮容斥,太麻煩了。

#include<iostream>
#include<algorithm>
#include<bitset>
//#include<unordered_map>
#include<fstream>
#include<iomanip>
#include
<string> #include<cmath> #include<cstring> #include<vector> #include<map> #include<set> #include<list> #include<queue> #include<stack> #include<sstream> #include<cstdio> #include<ctime> #include<cstdlib> #define INF 0x3f3f3f3f #define
inf 0x7FFFFFFF #define MOD 1000000007 #define moD 1000000003 #define pii pair<int,int> #define eps 1e-8 #define equals(a,b) (fabs(a-b)<eps) #define bug puts("bug") #define re register #define fi first #define se second const int maxn = 1e5 + 5; const double Inf = 10000.0; const double PI = acos(-1.0); typedef
long long ll; using namespace std; struct Point { double x, y; Point() { x = y = 0; } Point(double _x, double _y) { x = _x; y = _y; } }; bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } Point get_o(Point a, Point b, Point c) { double x1 = a.x, y1 = a.y; double x2 = b.x, y2 = b.y; double x3 = c.x, y3 = c.y; double a1 = 2 * (x2 - x1); double b1 = 2 * (y2 - y1); double c1 = x2 * x2 + y2 * y2 - x1 * x1 - y1 * y1; double a2 = 2 * (x3 - x2); double b2 = 2 * (y3 - y2); double c2 = x3 * x3 + y3 * y3 - x2 * x2 - y2 * y2; double x = (c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1); double y = (a1 * c2 - a2 * c1) / (a1 * b2 - a2 * b1); return Point(x, y); } bool check(Point a, Point b, Point c) { if (equals((b.y - a.y) * (c.x - a.x), (c.y - a.y) * (b.x - a.x))) return 0; return 1; } bool eq(Point a, Point b) { return equals(a.x, b.x) && equals(a.y, b.y); } vector<Point> v; Point pp[maxn]; int main() { int n; scanf("%d", &n); double x, y; Point p (0,0); for (int i = 0; i < n; i++) { scanf("%lf%lf", &x, &y); pp[i].x = x, pp[i].y = y; } if (n <= 2) { printf("%d",n); return 0; } int Max = 1; for (int i = 0; i < n; i++) { v.clear(); for (int j = i + 1; j < n; j++) { if (check(p, pp[i], pp[j])) v.push_back(get_o(p, pp[i], pp[j])); } sort(v.begin(), v.end()); int cnt = 2; for (int i = 1; i < v.size(); i++) { if (eq(v[i - 1], v[i])) cnt++, Max = max(Max, cnt); else Max = max(Max, cnt), cnt = 2; } } printf("%d", Max); }