Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f64b4bb

Browse files
Create 3464.Maximize-the-Distance-Between-Points-on-a-Square.cpp
1 parent 580cd2a commit f64b4bb

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using ll = long long;
2+
class Solution {
3+
vector<ll>arr;
4+
int next[15000];
5+
int n;
6+
ll side;
7+
public:
8+
ll pos(int j) {
9+
if (j<n)
10+
return arr[j];
11+
else
12+
return arr[j%n] + side*4;
13+
}
14+
15+
bool isOK(int dist, int k) {
16+
int j = 0;
17+
for (int i=0; i<n; i++) {
18+
while (pos(j)-arr[i]<dist)
19+
j++;
20+
next[i] = j;
21+
}
22+
23+
for (int i=0; i<n; i++) {
24+
int flag = true;
25+
int cur = i;
26+
for (int t=0; t<k-1; t++) {
27+
if (cur<n)
28+
cur = next[cur];
29+
else
30+
cur = next[cur%n] + n;
31+
if (cur >= i+n) {
32+
flag = false;
33+
break;
34+
}
35+
}
36+
if (pos(i)-pos(cur%n)<dist) {
37+
flag =false;
38+
}
39+
if (flag) {
40+
return true;
41+
}
42+
}
43+
return false;
44+
}
45+
46+
47+
int maxDistance(int side, vector<vector<int>>& points, int k) {
48+
this->n = points.size();
49+
this->side = side;
50+
for (auto& p: points) {
51+
if (p[0]==0)
52+
arr.push_back(p[1]);
53+
else if (p[1]==side)
54+
arr.push_back(side+p[0]);
55+
else if (p[0]==side)
56+
arr.push_back(2ll*side+side-p[1]);
57+
else if (p[1]==0)
58+
arr.push_back(3ll*side+side-p[0]);
59+
}
60+
61+
sort(arr.begin(), arr.end());
62+
63+
int low = 0, high = side;
64+
while (low < high) {
65+
int mid = high - (high-low)/2;
66+
if (isOK(mid, k))
67+
low = mid;
68+
else
69+
high = mid-1;
70+
}
71+
return low;
72+
}
73+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /