Skip to content

Commit 591ea43

Browse files
authored
Merge pull request #1651 from AlgorithmWithGod/0224LJH
[20251212] BOJ / P5 / 울타리 / 이종환
2 parents bd3569e + 8c45364 commit 591ea43

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

0224LJH/202512/12 BOJ 울타리.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
7+
static int treeCnt, ans;
8+
static Tree[] trees;
9+
static Tree[] sortedByX;
10+
static Tree[] sortedByY;
11+
12+
static class Tree {
13+
int y, x, cost;
14+
15+
public Tree(int y, int x, int cost) {
16+
this.y = y;
17+
this.x = x;
18+
this.cost = cost;
19+
}
20+
}
21+
22+
public static void main(String[] args) throws IOException {
23+
init();
24+
process();
25+
print();
26+
}
27+
28+
private static void init() throws IOException {
29+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
30+
treeCnt = Integer.parseInt(br.readLine());
31+
trees = new Tree[treeCnt];
32+
ans = treeCnt-1;
33+
34+
for (int i = 0; i < treeCnt; i++) {
35+
StringTokenizer st = new StringTokenizer(br.readLine());
36+
int x = Integer.parseInt(st.nextToken());
37+
int y = Integer.parseInt(st.nextToken());
38+
int cost = Integer.parseInt(st.nextToken());
39+
trees[i] = new Tree(y, x, cost);
40+
}
41+
42+
// x 기준 정렬
43+
sortedByX = Arrays.copyOf(trees, treeCnt);
44+
Arrays.sort(sortedByX, (a, b) -> a.x - b.x);
45+
46+
// y 기준 정렬
47+
sortedByY = Arrays.copyOf(trees, treeCnt);
48+
Arrays.sort(sortedByY, (a, b) -> a.y - b.y);
49+
}
50+
51+
private static void process() {
52+
// 모든 가능한 직사각형에 대해 시도
53+
for (int i = 0; i < treeCnt; i++) {
54+
for (int j = i; j < treeCnt; j++) {
55+
for (int k = 0; k < treeCnt; k++) {
56+
for (int l = k; l < treeCnt; l++) {
57+
int minX = sortedByX[i].x;
58+
int maxX = sortedByX[j].x;
59+
int minY = sortedByY[k].y;
60+
int maxY = sortedByY[l].y;
61+
62+
checkRectangle(minX, maxX, minY, maxY);
63+
}
64+
}
65+
}
66+
}
67+
}
68+
69+
private static void checkRectangle(int minX, int maxX, int minY, int maxY) {
70+
// 둘레 계산
71+
int width = maxX - minX;
72+
int height = maxY - minY;
73+
int perimeter = (width + height) * 2;
74+
75+
// 직사각형 내부의 나무들 찾기
76+
List<Integer> insideCosts = new ArrayList<>();
77+
int sum = 0;
78+
int cutCount = 0;
79+
for (int i = 0; i < treeCnt; i++) {
80+
if (trees[i].x >= minX && trees[i].x <= maxX &&
81+
trees[i].y >= minY && trees[i].y <= maxY) {
82+
insideCosts.add(trees[i].cost);
83+
} else {
84+
sum += trees[i].cost;
85+
cutCount++;
86+
}
87+
}
88+
89+
if (sum >= perimeter) {
90+
ans = Math.min(ans, cutCount);
91+
return;
92+
}
93+
94+
// cost 큰 순으로 정렬
95+
Collections.sort(insideCosts, Collections.reverseOrder());
96+
97+
for (int cost : insideCosts) {
98+
sum += cost;
99+
cutCount++;
100+
if (sum >= perimeter) {
101+
ans = Math.min(ans, cutCount);
102+
break;
103+
}
104+
}
105+
}
106+
107+
private static void print() {
108+
System.out.println(ans);
109+
}
110+
}
111+
```

0 commit comments

Comments
 (0)