Skip to content

Commit c0072ef

Browse files
authored
Merge pull request #44 from AlgorithmWithGod/khj20006
[20250205] BOJ / 플래3 / 달리기 코스 / 권혁준
2 parents 6364aa3 + 1f08ed8 commit c0072ef

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Point{
7+
long x,y;
8+
Point(long x, long y){
9+
this.x = x;
10+
this.y = y;
11+
}
12+
}
13+
14+
class Main {
15+
16+
// IO field
17+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
19+
static StringTokenizer st;
20+
21+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
22+
static int nextInt() {return Integer.parseInt(st.nextToken());}
23+
static long nextLong() {return Long.parseLong(st.nextToken());}
24+
static void bwEnd() throws Exception {bw.flush();bw.close();}
25+
26+
// Additional field
27+
static Point[] arr;
28+
static Point[] convexHull;
29+
30+
31+
static int N;
32+
static long ans = 0;
33+
34+
// positive : ccw
35+
static int ccw(Point a, Point b, Point c) {
36+
long res = (a.x*b.y + b.x*c.y + c.x*a.y) - (b.x*a.y + c.x*b.y + a.x*c.y);
37+
if(res > 0) return 1;
38+
if(res < 0) return -1;
39+
return 0;
40+
}
41+
42+
public static void main(String[] args) throws Exception {
43+
44+
input();
45+
46+
if(N <= 2) {
47+
if(N == 1) bw.write("0");
48+
else {
49+
long dx = arr[0].x - arr[1].x;
50+
long dy = arr[0].y - arr[1].y;
51+
bw.write((dx*dx+dy*dy) + "\n");
52+
}
53+
bwEnd();
54+
return;
55+
}
56+
57+
constructConvexHull();
58+
59+
rotate();
60+
61+
bw.write(ans+"\n");
62+
63+
bwEnd();
64+
}
65+
66+
static void input() throws Exception {
67+
nextLine();
68+
N = nextInt();
69+
arr = new Point[N];
70+
for(int i=0;i<N;i++) {
71+
nextLine();
72+
arr[i] = new Point(nextLong(), nextLong());
73+
}
74+
}
75+
76+
static void constructConvexHull() {
77+
78+
Arrays.sort(arr, (a,b) -> {
79+
if(a.x==b.x) return (int)(a.y-b.y);
80+
return (int)(a.x-b.x);
81+
});
82+
List<Point> lowerHull = new ArrayList<>();
83+
List<Point> upperHull = new ArrayList<>();
84+
for(Point p : arr) {
85+
while(lowerHull.size()>1 && ccw(lowerHull.get(lowerHull.size()-2), lowerHull.get(lowerHull.size()-1), p) <= 0) lowerHull.remove(lowerHull.size()-1);
86+
lowerHull.add(p);
87+
while(upperHull.size()>1 && ccw(upperHull.get(upperHull.size()-2), upperHull.get(upperHull.size()-1), p) >= 0) upperHull.remove(upperHull.size()-1);
88+
upperHull.add(p);
89+
}
90+
91+
convexHull = new Point[lowerHull.size() + upperHull.size() - 2];
92+
int idx = 0;
93+
for(Point p : upperHull) convexHull[idx++] = p;
94+
idx--;
95+
for(int i=lowerHull.size()-1;i>0;i--) convexHull[idx++] = lowerHull.get(i);
96+
97+
}
98+
99+
static void rotate() {
100+
int p = 1, sz = convexHull.length;
101+
for(int i=0;i<sz;i++) {
102+
long dx = convexHull[p%sz].x - convexHull[(i+1)%sz].x;
103+
long dy = convexHull[p%sz].y - convexHull[(i+1)%sz].y;
104+
ans = Math.max(ans, dist(convexHull[i],convexHull[p%sz]));
105+
while(p < 2*sz && ccw(convexHull[i], convexHull[(i+1)%sz], new Point(convexHull[(p+1)%sz].x - dx, convexHull[(p+1)%sz].y - dy)) <= 0) {
106+
p++;
107+
ans = Math.max(ans, dist(convexHull[i],convexHull[p%sz]));
108+
dx = convexHull[p%sz].x - convexHull[(i+1)%sz].x;
109+
dy = convexHull[p%sz].y - convexHull[(i+1)%sz].y;
110+
}
111+
}
112+
}
113+
114+
static long dist(Point a, Point b) {
115+
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
116+
}
117+
118+
}
119+
120+
```

0 commit comments

Comments
 (0)