0%

[題解]Leetcode 1453 Maximum Number of Darts Inside of a Circular Dartboard

1453 Maximum Number of Darts Inside of a Circular Dartboard

題目連結

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define ld long double
#define pdd pair<ld,ld>
#define x first
#define y second
#define exp 1e-6

class Solution {
public:

double dis(pdd a,pdd b){
ld x = a.x - b.x,y = a.y - b.y;
return sqrt(x * x + y * y);
}

pair<pdd,pdd> get_center(pdd a,pdd b,ld R){
pdd mid = {(a.x + b.x) / 2,(a.y + b.y) / 2};
ld theta = atan2(a.y - b.y, b.x - a.x);
ld tmp = dis(a,b) / 2, d = sqrt(R * R - tmp * tmp);

pair<pdd,pdd> ans;
ans.x = {mid.x - d * sin(theta),mid.y - d * cos(theta)};
ans.y = {mid.x + d * sin(theta),mid.y + d * cos(theta)};
return ans;
}

int numPoints(vector<vector<int>>& point, int R) {
int n = point.size(),ans = 1;
pdd p[n];for(int i=0;i<n;i++){p[i] = {point[i][0],point[i][1]};}
for(int i = 0;i < n;i++){
for(int j = i+1;j < n;j++){
if(dis(p[i],p[j]) - 2.0 * R >= exp)continue;
pair<pdd,pdd> cur = get_center(p[i],p[j],R);
int cnt = 0;
for(int k = 0;k < n;k++)
if(dis(p[k], cur.x) - R<= exp)cnt ++;
ans = max(ans, cnt);cnt = 0;
for(int k = 0;k < n;k++)
if(dis(p[k], cur.y) - R <= exp)cnt ++;
ans = max(ans, cnt);
}
}
return ans;
}
};