Skip to content

Commit ffd5ed1

Browse files
committed
240117
1 parent 15673a8 commit ffd5ed1

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package graph.tree;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
7+
/**
8+
* [문제 분석]
9+
* 이진 검색 트리를 전위 순회한 결과가 주어졌을 때, 이 트리를 후위 순회한 결과를 구하라
10+
*
11+
* [문제 풀이]
12+
*
13+
*/
14+
public class BOJ_5639_이진검색트리 {
15+
16+
static class Node {
17+
int num;
18+
Node left, right;
19+
20+
public Node(int num) {
21+
this.num = num;
22+
}
23+
24+
public Node(int num, Node left, Node right) {
25+
this.num = num;
26+
this.left = left;
27+
this.right = right;
28+
}
29+
30+
public void insert(int n) {
31+
if (n < this.num) {
32+
if (this.left == null) {
33+
this.left = new Node(n);
34+
} else {
35+
this.left.insert(n);
36+
}
37+
} else {
38+
if (this.right == null) {
39+
this.right = new Node(n);
40+
} else {
41+
this.right.insert(n);
42+
}
43+
}
44+
}
45+
}
46+
47+
public static void main(String[] args) throws IOException {
48+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
49+
50+
// 루트 노드 설정
51+
Node root = new Node(Integer.parseInt(br.readLine()));
52+
53+
// 트리 구성
54+
String input;
55+
while (true) {
56+
input = br.readLine();
57+
if (input == null || input.equals("")) break;
58+
59+
root.insert(Integer.parseInt(input));
60+
}
61+
62+
postOrder(root);
63+
}
64+
65+
// 후위순회: 왼쪽자식-오른쪽자식-부모
66+
private static void postOrder(Node node) {
67+
if (node == null) return;
68+
69+
postOrder(node.left);
70+
postOrder(node.right);
71+
System.out.println(node.num);
72+
}
73+
74+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package implementation;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.StringTokenizer;
8+
9+
/**
10+
* [문제 분석]
11+
* (x, y), (x+1, y-1), ..., (x+d1, y-d1)
12+
* (x, y), (x+1, y+1), ..., (x+d2, y+d2)
13+
* (x+d1, y-d1), (x+d1+1, y-d1+1), ... (x+d1+d2, y-d1+d2)
14+
* (x+d2, y+d2), (x+d2+1, y+d2-1), ..., (x+d2+d1, y+d2-d1)
15+
* 경계선과 경계선의 안에 포함되어있는 곳은 5번 선거구이다.
16+
*
17+
* 1번 선거구: 1 ≤ r < x+d1, 1 ≤ c ≤ y
18+
* 2번 선거구: 1 ≤ r ≤ x+d2, y < c ≤ N
19+
* 3번 선거구: x+d1 ≤ r ≤ N, 1 ≤ c < y-d1+d2
20+
* 4번 선거구: x+d2 < r ≤ N, y-d1+d2 ≤ c ≤ N
21+
*
22+
* 조건)
23+
* d1, d2 ≥ 1
24+
* 1 ≤ x < x+d1+d2 ≤ N
25+
* 1 ≤ y-d1 < y < y+d2 ≤ N
26+
*
27+
* [문제 풀이]
28+
* x, y, d1, d2에 대해 모든 경우의 수를 탐색하여
29+
* 최솟값을 찾는다
30+
*/
31+
public class BOJ_17779_게리맨더링2 {
32+
33+
static int N, total;
34+
static int[][] map;
35+
36+
public static void main(String[] args) throws IOException {
37+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
38+
StringTokenizer st;
39+
40+
N = Integer.parseInt(br.readLine());
41+
map = new int[N][N];
42+
for (int i = 0; i < N; i++) {
43+
st = new StringTokenizer(br.readLine());
44+
for (int j = 0; j < N; j++) {
45+
map[i][j] = Integer.parseInt(st.nextToken());
46+
total += map[i][j];
47+
}
48+
}
49+
50+
System.out.println(solution());
51+
}
52+
53+
private static int solution() {
54+
int min = Integer.MAX_VALUE;
55+
56+
for (int x = 0; x < N; x++) {
57+
for (int y = 0; y < N; y++) {
58+
for (int d1 = 1; d1 < N; d1++) {
59+
for (int d2 = 1; d2 < N; d2++) {
60+
if (x + d1 + d2 >= N) continue;
61+
if (y - d1 < 0 || y + d2 >= N) continue;
62+
63+
min = Math.min(min, simulation(x, y, d1, d2));
64+
}
65+
}
66+
}
67+
}
68+
69+
return min;
70+
}
71+
72+
private static int simulation(int x, int y, int d1, int d2) {
73+
boolean[][] border = new boolean[N][N];
74+
75+
// 경계선 마킹
76+
for (int i = 0; i <= d1; i++) {
77+
border[x+i][y-i] = true;
78+
border[x+d2+i][y+d2-i] = true;
79+
}
80+
for (int i = 0; i <= d2; i++) {
81+
border[x+i][y+i] = true;
82+
border[x+d1+i][y-d1+i] = true;
83+
}
84+
85+
int[] people = new int[5]; // 구역별 인구수
86+
87+
// 1 선거구
88+
for (int r = 0; r < x+d1; r++) {
89+
for (int c = 0; c <= y; c++) {
90+
if (border[r][c]) break;
91+
people[0] += map[r][c];
92+
}
93+
}
94+
95+
// 2 선거구
96+
for (int r = 0; r <= x+d2; r++) {
97+
for (int c = N-1; c > y; c--) {
98+
if (border[r][c]) break;
99+
people[1] += map[r][c];
100+
}
101+
}
102+
103+
// 3 선거구
104+
for (int r = x+d1; r < N; r++) {
105+
for (int c = 0; c < y-d1+d2; c++) {
106+
if (border[r][c]) break;
107+
people[2] += map[r][c];
108+
}
109+
}
110+
111+
// 4 선거구
112+
for (int r = x+d2+1; r < N; r++) {
113+
for (int c = N-1; c >= y-d1+d2; c--) {
114+
if (border[r][c]) break;
115+
people[3] += map[r][c];
116+
}
117+
}
118+
119+
// 5 선거구
120+
people[4] = total;
121+
for (int i = 0; i <= 3; i++) {
122+
people[4] -= people[i];
123+
}
124+
125+
Arrays.sort(people);
126+
127+
return people[4] - people[0];
128+
}
129+
130+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package twoPointer;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
8+
public class BOJ_2470_두용액 {
9+
10+
static int N;
11+
static int[] nums;
12+
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
16+
N = Integer.parseInt(br.readLine());
17+
nums = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
18+
19+
solution();
20+
}
21+
22+
private static void solution() {
23+
Arrays.sort(nums);
24+
25+
int leftIdx = 0;
26+
int rightIdx = N-1;
27+
int leftValue = nums[leftIdx];
28+
int rightValue = nums[rightIdx];
29+
int gap = Integer.MAX_VALUE;
30+
31+
int sum;
32+
int abs;
33+
while (leftIdx < rightIdx) {
34+
sum = nums[leftIdx] + nums[rightIdx];
35+
abs = Math.abs(sum);
36+
if (abs < gap) {
37+
gap = abs;
38+
leftValue = nums[leftIdx];
39+
rightValue = nums[rightIdx];
40+
}
41+
42+
if (sum > 0) {
43+
rightIdx--;
44+
} else {
45+
leftIdx++;
46+
}
47+
}
48+
49+
System.out.println(leftValue + " " + rightValue);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)