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 cf64aee

Browse files
Merge pull request #278 from Jewan1120/main
[20주차] 백제완
2 parents 2ec5ac3 + 0a45a69 commit cf64aee

File tree

10 files changed

+448
-0
lines changed

10 files changed

+448
-0
lines changed

‎BOJ/1000-5000번/JW_1967_2.java‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.ArrayList;
2+
3+
public class JW_1967_2 {
4+
5+
static int n;
6+
static ArrayList<ArrayList<int[]>> tree = new ArrayList<>();
7+
static boolean[] visited;
8+
static int maxNode = 0, maxWeight = 0;
9+
10+
public static void main(String[] args) throws Exception {
11+
n = read();
12+
for (int i = 0; i < n + 1; i++)
13+
tree.add(new ArrayList<>());
14+
for (int i = 0; i < n - 1; i++) {
15+
int u = read(), v = read(), w = read();
16+
tree.get(u).add(new int[] { v, w });
17+
tree.get(v).add(new int[] { u, w });
18+
}
19+
visited = new boolean[n + 1];
20+
dfs(1, 0); // 루트로부터 가장 멀리 떨어진 노드 찾기
21+
visited = new boolean[n + 1]; // 방문 배열 초기화
22+
dfs(maxNode, 0); // 해당 노드로부터 가장 멀리 떨어진 노드 찾
23+
System.out.println(maxWeight);
24+
}
25+
26+
private static void dfs(int node, int weight) {
27+
// 최댓값 갱신
28+
if (weight > maxWeight) {
29+
maxNode = node;
30+
maxWeight = weight;
31+
}
32+
visited[node] = true; // 방문 처리
33+
for (int[] edge : tree.get(node)) {
34+
if (visited[edge[0]])
35+
continue;
36+
dfs(edge[0], weight + edge[1]);
37+
}
38+
}
39+
40+
private static int read() throws Exception {
41+
int c, n = System.in.read() & 15;
42+
while ((c = System.in.read()) >= 48)
43+
n = (n << 3) + (n << 1) + (c & 15);
44+
if (c == 13)
45+
System.in.read();
46+
return n;
47+
}
48+
}

‎BOJ/1000-5000번/JW_2110_2.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package boj;
2+
3+
import java.util.Arrays;
4+
5+
public class JW_2110_2 {
6+
7+
static int n, c;
8+
static int[] arr;
9+
10+
public static void main(String[] args) throws Exception {
11+
n = read();
12+
c = read();
13+
arr = new int[n];
14+
for (int i = 0; i < n; i++)
15+
arr[i] = read();
16+
Arrays.sort(arr);
17+
int l = 1, r = arr[n - 1];
18+
while (l <= r) {
19+
int m = (l + r) / 2;
20+
if (isPossible(m))
21+
l = m + 1;
22+
else
23+
r = m - 1;
24+
}
25+
System.out.println(r);
26+
}
27+
28+
// 결정 함수
29+
private static boolean isPossible(int target) {
30+
int cnt = 1, prev = 0;
31+
for (int i = 1; i < n; i++)
32+
if (arr[i] - arr[prev] >= target) {
33+
cnt++;
34+
prev = i;
35+
}
36+
return cnt >= c;
37+
}
38+
39+
private static int read() throws Exception {
40+
int c, n = System.in.read() & 15;
41+
while ((c = System.in.read()) >= 48)
42+
n = (n << 3) + (n << 1) + (c & 15);
43+
if (c == 13)
44+
System.in.read();
45+
return n;
46+
}
47+
}

‎BOJ/1000-5000번/JW_2805_2.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class JW_2805_2 {
2+
3+
static int n, m;
4+
static int[] arr;
5+
6+
public static void main(String[] args) throws Exception {
7+
n = read();
8+
m = read();
9+
arr = new int[n];
10+
int l = 0, r = 0;
11+
for (int i = 0; i < n; i++) {
12+
arr[i] = read();
13+
r = Math.max(r, arr[i]);
14+
}
15+
// 매개변수 탐색
16+
while (l <= r) {
17+
int target = (l + r) / 2;
18+
if (isPossible(target))
19+
l = target + 1;
20+
else
21+
r = target - 1;
22+
}
23+
System.out.println(r);
24+
}
25+
26+
// 결정함수
27+
private static boolean isPossible(int target) {
28+
long sum = 0;
29+
for (int i = 0; i < n; i++) {
30+
sum += Math.max(0, arr[i] - target);
31+
if (sum >= m)
32+
return true;
33+
}
34+
return false;
35+
}
36+
37+
private static int read() throws Exception {
38+
int c, n = System.in.read() & 15;
39+
while ((c = System.in.read()) >= 48)
40+
n = (n << 3) + (n << 1) + (c & 15);
41+
if (c == 13)
42+
System.in.read();
43+
return n;
44+
}
45+
}

‎BOJ/1000-5000번/JW_7579_2.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class JW_7579_2 {
2+
3+
static int n, m;
4+
static int[] mArr;
5+
static int[] cArr;
6+
7+
public static void main(String[] args) throws Exception {
8+
n = read();
9+
m = read();
10+
mArr = new int[n];
11+
cArr = new int[n];
12+
for (int i = 0; i < n; i++)
13+
mArr[i] = read();
14+
int maxCost = 0;
15+
for (int i = 0; i < n; i++) {
16+
cArr[i] = read();
17+
maxCost += cArr[i];
18+
}
19+
int[] dp = new int[maxCost + 1]; // 냅색 DP
20+
int minCost = Integer.MAX_VALUE;
21+
// 1차원 냅색
22+
for (int i = 0; i < n; i++)
23+
for (int j = maxCost; j >= cArr[i]; j--) {
24+
dp[j] = Math.max(dp[j], dp[j - cArr[i]] + mArr[i]);
25+
if (dp[j] >= m)
26+
minCost = Math.min(minCost, j);
27+
}
28+
System.out.println(minCost);
29+
}
30+
31+
private static int read() throws Exception {
32+
int c, n = System.in.read() & 15;
33+
while ((c = System.in.read()) >= 48)
34+
n = (n << 3) + (n << 1) + (c & 15);
35+
if (c == 13)
36+
System.in.read();
37+
return n;
38+
}
39+
}

‎BOJ/5001-10000번/JW_5021_2.java‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Deque;
6+
import java.util.HashMap;
7+
import java.util.StringTokenizer;
8+
9+
public class JW_5021_2 {
10+
static HashMap<String, Double> bloodMap = new HashMap<>();
11+
static HashMap<String, ArrayList<String>> childMap = new HashMap<>();
12+
static HashMap<String, String[]> parentMap = new HashMap<>();
13+
static HashMap<String, Integer> indegreeMap = new HashMap<>();
14+
15+
public static void main(String[] args) throws Exception {
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
int n = Integer.parseInt(st.nextToken());
19+
int m = Integer.parseInt(st.nextToken());
20+
bloodMap.put(br.readLine(), 1D);
21+
for (int i = 0; i < n; i++) {
22+
st = new StringTokenizer(br.readLine());
23+
String child = st.nextToken();
24+
String parent1 = st.nextToken();
25+
String parent2 = st.nextToken();
26+
indegreeMap.put(child, 2);
27+
indegreeMap.putIfAbsent(parent1, 0);
28+
indegreeMap.putIfAbsent(parent2, 0);
29+
childMap.putIfAbsent(parent1, new ArrayList<>());
30+
childMap.putIfAbsent(parent2, new ArrayList<>());
31+
childMap.get(parent1).add(child);
32+
childMap.get(parent2).add(child);
33+
parentMap.put(child, new String[] { parent1, parent2 });
34+
}
35+
// 위상 정렬
36+
Deque<String> dq = new ArrayDeque<>();
37+
for (String name : indegreeMap.keySet())
38+
// 위상 정렬 초기화
39+
if (indegreeMap.get(name) == 0)
40+
dq.offer(name);
41+
while (!dq.isEmpty()) {
42+
String name = dq.poll();
43+
// 부모가 없는 경우에는 왕족이 아님
44+
if (!parentMap.containsKey(name))
45+
bloodMap.putIfAbsent(name, 0D);
46+
else {
47+
String[] parents = parentMap.get(name);
48+
bloodMap.put(name, bloodMap.get(parents[0]) / 2 + bloodMap.get(parents[1]) / 2); // 혈통 계산
49+
}
50+
// 진입 차수 줄이기
51+
for (String child : childMap.getOrDefault(name, new ArrayList<String>())) {
52+
indegreeMap.put(child, indegreeMap.get(child) - 1);
53+
// 진입 차수가 0이라면, 큐(덱)에 삽입
54+
if (indegreeMap.get(child) == 0)
55+
dq.offer(child);
56+
}
57+
}
58+
String next = "";
59+
double maxBlood = -1;
60+
while (m-- > 0) {
61+
String name = br.readLine();
62+
if (bloodMap.getOrDefault(name, 0D) > maxBlood) {
63+
next = name;
64+
maxBlood = bloodMap.getOrDefault(name, 0D);
65+
}
66+
}
67+
System.out.println(next);
68+
}
69+
}

‎BOJ/5001-10000번/JW_9084_2.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class JW_9084_2 {
2+
3+
public static void main(String[] args) throws Exception {
4+
int t = read();
5+
StringBuilder sb = new StringBuilder();
6+
while (t-- > 0) {
7+
int n = read();
8+
int[] money = new int[n];
9+
for (int i = 0; i < n; i++)
10+
money[i] = read();
11+
int m = read();
12+
int[] dp = new int[m + 1];
13+
dp[0] = 1;
14+
for (int i = 0; i < n; i++)
15+
for (int j = money[i]; j <= m; j++)
16+
dp[j] += dp[j - money[i]];
17+
sb.append(dp[m]).append("\n");
18+
}
19+
System.out.println(sb);
20+
}
21+
22+
private static int read() throws Exception {
23+
int c, n = System.in.read() & 15;
24+
while ((c = System.in.read()) >= 48)
25+
n = (n << 3) + (n << 1) + (c & 15);
26+
if (c == 13)
27+
System.in.read();
28+
return n;
29+
}
30+
}

‎BOJ/5001-10000번/JW_9205_2.java‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.util.ArrayDeque;
2+
import java.util.ArrayList;
3+
import java.util.Deque;
4+
5+
public class JW_9205_2 {
6+
7+
static int n;
8+
static int[][] distance;
9+
static ArrayList<ArrayList<Integer>> graph; // 간선 정보
10+
11+
public static void main(String[] args) throws Exception {
12+
int t = read();
13+
StringBuilder sb = new StringBuilder();
14+
while (t-- > 0) {
15+
n = read();
16+
distance = new int[n + 2][];
17+
for (int i = 0; i < n + 2; i++)
18+
distance[i] = new int[] { read(), read() };
19+
graph = new ArrayList<>();
20+
for (int i = 0; i < n + 2; i++)
21+
graph.add(new ArrayList<>());
22+
// 간선 정보 입력
23+
for (int i = 0; i < n + 1; i++)
24+
for (int j = i + 1; j < n + 2; j++)
25+
if (isMovable(distance[i], distance[j])) {
26+
graph.get(i).add(j);
27+
graph.get(j).add(i);
28+
}
29+
if (isPossible())
30+
sb.append("happy\n");
31+
else
32+
sb.append("sad\n");
33+
}
34+
System.out.println(sb);
35+
}
36+
37+
// 페스티벌 좌표(n+1) 까지 이동할 수 있는지 BFS
38+
private static boolean isPossible() {
39+
Deque<Integer> dq = new ArrayDeque<>();
40+
boolean[] visited = new boolean[n + 2];
41+
dq.offer(0);
42+
visited[0] = true;
43+
while (!dq.isEmpty()) {
44+
int cur = dq.poll();
45+
// 종료 조건
46+
if (cur == n + 1)
47+
return true;
48+
for (int next : graph.get(cur))
49+
if (!visited[next]) {
50+
dq.offer(next);
51+
visited[next] = true;
52+
}
53+
}
54+
return false;
55+
}
56+
57+
// 맨해튼 거리로 이동할 수 있는지 확인
58+
private static boolean isMovable(int[] A, int[] B) {
59+
return Math.abs(A[0] - B[0]) + Math.abs(A[1] - B[1]) <= 1_000;
60+
}
61+
62+
private static int read() throws Exception {
63+
int c, n = System.in.read() & 15;
64+
boolean m = n == 13;
65+
if (m)
66+
n = System.in.read() & 15;
67+
while ((c = System.in.read()) >= 48)
68+
n = (n << 3) + (n << 1) + (c & 15);
69+
if (c == 13)
70+
System.in.read();
71+
return m ? ~n + 1 : n;
72+
}
73+
}

0 commit comments

Comments
(0)

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