Skip to content

Commit 2e5b4a7

Browse files
committed
Merge branch 'week4'
2 parents 9add9e9 + 391d363 commit 2e5b4a7

8 files changed

+281
-0
lines changed

Diff for: README.md

+10
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@
2020
| [요세푸스 문제 0](https://www.acmicpc.net/problem/11866) | [BOJ11866.java](https://github.com/java-algorithm/bom/commit/e1f5552b1db29c4c8c6e3172c8d2aa6e616b4320) |
2121
| [트럭](https://www.acmicpc.net/problem/13335) | [BOJ13335.java](https://github.com/java-algorithm/bom/commit/1b6db9953e841d1f262ae2bd753ee77d1d818012) |
2222
| [마인크래프트](https://www.acmicpc.net/problem/18111) | [BOJ18111.java](https://github.com/java-algorithm/bom/commit/511de0c38cd73787c4e02780bad8668b24afd7b1) |
23+
24+
## 3주차 BFS/DFS
25+
| 문제 | 풀이 |
26+
|----------------------------------------------------|--------------------------------------------------------------------------------------------------------|
27+
| [안전 영역](https://www.acmicpc.net/problem/2468) | [BOJ2468.java](https://github.com/java-algorithm/bom/commit/9a2e2a4988ae917a5acc360b9deee8b5d4c3a064) |
28+
| [스타트링크](https://www.acmicpc.net/problem/5014) | [BOJ5014.java](https://github.com/java-algorithm/bom/commit/c768a6682dde154e31e120f0fb90b3a700bf5027) |
29+
| [경로 찾기](https://www.acmicpc.net/problem/11403) | [BOJ11403.java](https://github.com/java-algorithm/bom/commit/0e12aaf73bc283a0df77a5596400641b1a1afb3a) |
30+
| [트리의 부모 찾기](https://www.acmicpc.net/problem/11725) | [BOJ11725.java](https://github.com/java-algorithm/bom/commit/ddf2a95cf27270655d5ee86f4bf989e8a4c27058) |
31+
| [데스 나이트](https://www.acmicpc.net/problem/16948) | [BOJ16948.java](https://github.com/java-algorithm/bom/commit/fb7409184d603887746eb8f446d61444f24509a9) |
32+
| [아기 상어 2](https://www.acmicpc.net/problem/17086) | [BOJ17086.java](https://github.com/java-algorithm/bom/commit/c178c31cef1cc4164442d8e6910512ae2b24f7ee) |

Diff for: week4/BOJ_10989(수 정렬하기 3).java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.io.*;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) throws IOException {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
int N = Integer.parseInt(br.readLine());
8+
int[] arr = new int[N];
9+
int[] count = new int[10002];
10+
int[] sortedArr = new int[N];
11+
for(int i=0; i<N; i++) {
12+
arr[i] = Integer.parseInt(br.readLine());
13+
count[arr[i]]++;
14+
}
15+
for(int i=1; i<=10000; i++) {
16+
count[i] += count[i-1];
17+
}
18+
for(int i=0; i<N; i++) {
19+
int n = arr[i];
20+
count[n]--;
21+
sortedArr[count[n]] = n;
22+
}
23+
StringBuffer sb = new StringBuffer();
24+
for(int i=0; i<N; i++) {
25+
sb.append(sortedArr[i]).append('\n');
26+
}
27+
System.out.print(sb);
28+
}
29+
}

Diff for: week4/BOJ_1141(접두사).java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int N = Integer.parseInt(br.readLine());
9+
String[] str = new String[N];
10+
for(int i=0; i<N; i++) {
11+
str[i] = br.readLine();
12+
}
13+
Arrays.sort(str);
14+
int answer = 1;
15+
for(int i=0; i<N-1; i++) {
16+
if(!str[i+1].startsWith(str[i])) {
17+
answer++;
18+
}
19+
}
20+
System.out.println(answer);
21+
}
22+
}

Diff for: week4/BOJ_1744(수 묶기).java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
int N = Integer.parseInt(br.readLine());
10+
List<Integer> positiveNum = new ArrayList<>();
11+
List<Integer> negativeNum = new ArrayList<>();
12+
for(int i=0; i<N; i++) {
13+
int num = Integer.parseInt(br.readLine());
14+
if(num > 0) {
15+
positiveNum.add(num);
16+
}else {
17+
negativeNum.add(num);
18+
}
19+
}
20+
Collections.sort(positiveNum);
21+
Collections.sort(negativeNum, Collections.reverseOrder());
22+
int answer = getMaxBindSum(positiveNum) + getMaxBindSum(negativeNum);
23+
System.out.println(answer);
24+
}
25+
26+
public static int getMaxBindSum(List<Integer> list) {
27+
int max = 0;
28+
for (int i = list.size()-1; i >= 0; ) {
29+
if (i == 0) {
30+
max += list.get(i);
31+
break;
32+
}
33+
int multiply = list.get(i) * list.get(i-1);
34+
int plus = list.get(i) + list.get(i-1);
35+
if (multiply < list.get(i)) {
36+
max += list.get(i--);
37+
} else if (multiply < plus) {
38+
max += plus;
39+
i -= 2;
40+
} else {
41+
max += multiply;
42+
i -= 2;
43+
}
44+
}
45+
return max;
46+
}
47+
}

Diff for: week4/BOJ_18870(좌표 압축).java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int N = Integer.parseInt(br.readLine());
9+
StringTokenizer st = new StringTokenizer(br.readLine());
10+
int[] arr = new int[N];
11+
Set<Integer> set = new HashSet<>();
12+
for(int i=0; i<N; i++) {
13+
arr[i] = Integer.parseInt(st.nextToken());
14+
set.add(arr[i]);
15+
}
16+
List<Integer> list = new ArrayList<>(set);
17+
Collections.sort(list);
18+
Map<Integer, Integer> map = new HashMap<>();
19+
for(int i=0; i<list.size(); i++) {
20+
map.put(list.get(i), i);
21+
}
22+
StringBuffer sb = new StringBuffer();
23+
for(int i=0; i<N; i++) {
24+
sb.append(map.get(arr[i])).append(' ');
25+
}
26+
System.out.println(sb);
27+
}
28+
}

Diff for: week4/BOJ_2075(N번째 큰 수).java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int N = Integer.parseInt(br.readLine());
9+
PriorityQueue<Integer> q = new PriorityQueue<>((o1, o2) -> o2 - o1);
10+
for(int i=0; i<N; i++) {
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
for(int j=0; j<N; j++) {
13+
int num = Integer.parseInt(st.nextToken());
14+
q.offer(num);
15+
}
16+
}
17+
for(int i=0; i<N-1; i++) {
18+
q.poll();
19+
}
20+
System.out.println(q.peek());
21+
}
22+
}

Diff for: week4/BOJ_2473(세 용액).java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int N = Integer.parseInt(br.readLine());
9+
StringTokenizer st = new StringTokenizer(br.readLine());
10+
long[] arr = new long[N];
11+
for (int i = 0; i < N; i++) {
12+
arr[i] = Long.parseLong(st.nextToken());
13+
}
14+
Arrays.sort(arr);
15+
long minAbs = Math.abs(arr[0] + arr[1] + arr[2]);
16+
long[] answer = new long[]{arr[0], arr[1], arr[2]};
17+
for (int i = 0; i < N - 2; i++) {
18+
long[] result = twoPointer(arr, i);
19+
long resultAbs = Math.abs(result[0] + result[1] + result[2]);
20+
if (minAbs > resultAbs) {
21+
minAbs = resultAbs;
22+
answer = result.clone();
23+
}
24+
}
25+
System.out.println(answer[0] + " " + answer[1] + " " + answer[2]);
26+
}
27+
28+
public static long[] twoPointer(long[] arr, int index) {
29+
int left = index + 1;
30+
int right = arr.length - 1;
31+
int leftIndex = left;
32+
int rightIndex = right;
33+
long minAbs = Math.abs(arr[left] + arr[right] + arr[index]);
34+
while (left < right) {
35+
long sum = arr[index] + arr[left] + arr[right];
36+
if (minAbs > Math.abs(sum)) {
37+
minAbs = Math.abs(sum);
38+
leftIndex = left;
39+
rightIndex = right;
40+
}
41+
if (sum > 0) {
42+
right--;
43+
} else {
44+
left++;
45+
}
46+
}
47+
return new long[]{arr[index], arr[leftIndex], arr[rightIndex]};
48+
}
49+
}

Diff for: week4/BOJ_3758(KCPC).java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringBuffer sb = new StringBuffer();
9+
int T = Integer.parseInt(br.readLine());
10+
for(int i=0; i<T; i++) {
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
int totalTeam = Integer.parseInt(st.nextToken());
13+
int totalQuiz = Integer.parseInt(st.nextToken());
14+
int teamId = Integer.parseInt(st.nextToken());
15+
int logCount = Integer.parseInt(st.nextToken());
16+
17+
int[][] score = new int[totalTeam+1][totalQuiz+1];
18+
int[] submission = new int[totalTeam+1];
19+
int[] lastTime = new int[totalTeam+1];
20+
Arrays.fill(lastTime, logCount);
21+
List<Team> teamList = new ArrayList<>();
22+
for(int j=0; j<logCount; j++) {
23+
st = new StringTokenizer(br.readLine());
24+
int t = Integer.parseInt(st.nextToken());
25+
int q = Integer.parseInt(st.nextToken());
26+
int s = Integer.parseInt(st.nextToken());
27+
score[t][q] = Math.max(score[t][q], s);
28+
submission[t]++;
29+
lastTime[t] = j;
30+
}
31+
for(int j=1; j<=totalTeam; j++) {
32+
int totalScore = 0;
33+
for(int k=1; k<=totalQuiz; k++) {
34+
totalScore += score[j][k];
35+
}
36+
teamList.add(new Team(j, totalScore, submission[j], lastTime[j]));
37+
}
38+
Collections.sort(teamList);
39+
40+
for(int j=0; j<totalTeam; j++) {
41+
if(teamId == teamList.get(j).teamId) {
42+
sb.append(j+1).append('\n');
43+
break;
44+
}
45+
}
46+
}
47+
System.out.print(sb);
48+
}
49+
50+
public static class Team implements Comparable<Team>{
51+
int teamId;
52+
int score;
53+
int submissionCount;
54+
int lastTime;
55+
56+
public Team(int teamId, int score, int count, int time) {
57+
this.teamId = teamId;
58+
this.score = score;
59+
this.submissionCount = count;
60+
this.lastTime = time;
61+
}
62+
63+
@Override
64+
public int compareTo(Team t) {
65+
if(this.score == t.score) {
66+
if(t.submissionCount == this.submissionCount) {
67+
return Integer.compare(this.lastTime, t.lastTime);
68+
}
69+
return Integer.compare(this.submissionCount, t.submissionCount);
70+
}
71+
return Integer.compare(t.score, this.score);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)