Skip to content

Commit b60449b

Browse files
authored
[20250310] BOJ / P4 / 감옥 건설 / 권혁준
1 parent 713f72e commit b60449b

File tree

1 file changed

+116
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)