Skip to content

Commit f96b268

Browse files
committed
Merge branch 'week5'
2 parents 61d1423 + dbc19c6 commit f96b268

7 files changed

+326
-0
lines changed

week5/BOJ_1072(게임).java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static final int MAX = 1000000000;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
Long X = Long.parseLong(st.nextToken());
12+
Long Y = Long.parseLong(st.nextToken());
13+
int Z = (int) ((Y * 100) / X);
14+
if (Z >= 99) {
15+
System.out.println("-1");
16+
return;
17+
}
18+
int left = 1;
19+
int right = MAX;
20+
int answer = right;
21+
while (left <= right) {
22+
int mid = (left + right) / 2;
23+
if (((Y + mid) * 100 / (X + mid)) > Z) {
24+
right = mid - 1;
25+
answer = mid;
26+
} else {
27+
left = mid + 1;
28+
}
29+
}
30+
System.out.println(answer);
31+
}
32+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int M = Integer.parseInt(st.nextToken());
11+
List<String> S = new ArrayList<>();
12+
for(int i=0; i<N; i++) {
13+
S.add(br.readLine());
14+
}
15+
Collections.sort(S);
16+
List<String> strList = new ArrayList<>();
17+
for(int i=0; i<M; i++) {
18+
strList.add(br.readLine());
19+
}
20+
int count = 0;
21+
for(int i=0; i<M; i++) {
22+
int left = 0;
23+
int right = N-1;
24+
while(left <= right) {
25+
int mid = (left + right)/2;
26+
int compare = S.get(mid).compareTo(strList.get(i));
27+
if(compare >= 0) {
28+
if(S.get(mid).startsWith(strList.get(i))) {
29+
count++;
30+
break;
31+
}
32+
right = mid-1;
33+
}else {
34+
left = mid+1;
35+
}
36+
}
37+
}
38+
System.out.println(count);
39+
}
40+
}

week5/BOJ_1939(중량제한).java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static int N;
7+
public static List<List<int[]>> graph;
8+
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
N = Integer.parseInt(st.nextToken());
13+
int M = Integer.parseInt(st.nextToken());
14+
graph = new ArrayList<>();
15+
for (int i = 0; i <= N; i++) {
16+
graph.add(new ArrayList<>());
17+
}
18+
int max = 0;
19+
int min = Integer.MAX_VALUE;
20+
for (int i = 0; i < M; i++) {
21+
st = new StringTokenizer(br.readLine());
22+
int A = Integer.parseInt(st.nextToken());
23+
int B = Integer.parseInt(st.nextToken());
24+
int C = Integer.parseInt(st.nextToken());
25+
graph.get(A).add(new int[]{B, C});
26+
graph.get(B).add(new int[]{A, C});
27+
max = Math.max(max, C);
28+
min = Math.min(min, C);
29+
}
30+
st = new StringTokenizer(br.readLine());
31+
int start = Integer.parseInt(st.nextToken());
32+
int end = Integer.parseInt(st.nextToken());
33+
34+
int left = min;
35+
int right = max;
36+
int answer = left;
37+
while (left <= right) {
38+
int mid = (left + right) / 2;
39+
if (!checkPathWeight(start, end, mid)) {
40+
right = mid - 1;
41+
} else {
42+
answer = Math.max(answer, mid);
43+
left = mid + 1;
44+
}
45+
}
46+
System.out.println(answer);
47+
}
48+
49+
public static boolean checkPathWeight(int start, int end, int weight) {
50+
boolean[] visited = new boolean[N + 1];
51+
Queue<Integer> q = new LinkedList<>();
52+
q.offer(start);
53+
while (!q.isEmpty()) {
54+
int v = q.poll();
55+
if (v == end) {
56+
return true;
57+
}
58+
List<int[]> list = graph.get(v);
59+
for (int i = 0; i < list.size(); i++) {
60+
int[] edge = list.get(i);
61+
int vertex = edge[0];
62+
if (edge[1] > 0 && edge[1] >= weight && !visited[vertex]) {
63+
visited[vertex] = true;
64+
q.offer(vertex);
65+
}
66+
}
67+
}
68+
return false;
69+
}
70+
}

week5/BOJ_2110(공유기 설치).java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int C = Integer.parseInt(st.nextToken());
11+
12+
int[] house = new int[N];
13+
for (int i = 0; i < N; i++) {
14+
house[i] = Integer.parseInt(br.readLine());
15+
}
16+
Arrays.sort(house);
17+
int left = 1;
18+
int right = house[N - 1] - house[0];
19+
int answer = 0;
20+
while (left <= right) {
21+
int mid = (left + right) / 2;
22+
int index = 0;
23+
int count = 1;
24+
for (int i = 0; i < N; i++) {
25+
if (house[i] - house[index] >= mid) {
26+
index = i;
27+
count++;
28+
}
29+
}
30+
if (count < C) {
31+
right = mid - 1;
32+
} else {
33+
answer = mid;
34+
left = mid + 1;
35+
}
36+
}
37+
System.out.println(answer);
38+
}
39+
}

week5/BOJ_2343(기타 레슨).java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int M = Integer.parseInt(st.nextToken());
11+
st = new StringTokenizer(br.readLine());
12+
int[] lectures = new int[N];
13+
int sum = 0;
14+
for (int i = 0; i < N; i++) {
15+
lectures[i] = Integer.parseInt(st.nextToken());
16+
sum += lectures[i];
17+
}
18+
int left = lectures[0];
19+
int right = sum;
20+
int answer = sum;
21+
while (left <= right) {
22+
int mid = (left + right) / 2;
23+
int time = 0;
24+
int count = 0;
25+
int maxTime = left;
26+
for (int i = 0; i < N; i++) {
27+
if (time + lectures[i] > mid) {
28+
maxTime = Math.max(maxTime, time);
29+
time = lectures[i];
30+
count++;
31+
} else {
32+
time += lectures[i];
33+
}
34+
}
35+
if (time > 0) {
36+
count++;
37+
maxTime = Math.max(maxTime, time);
38+
}
39+
if (count <= M) {
40+
answer = Math.min(answer, maxTime);
41+
right = mid - 1;
42+
} else {
43+
left = mid + 1;
44+
}
45+
}
46+
System.out.println(answer);
47+
}
48+
}

week5/BOJ_2792(보석 상자).java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int M = Integer.parseInt(st.nextToken());
11+
int[] jewel = new int[M];
12+
int max = Integer.MIN_VALUE;
13+
for (int i = 0; i < M; i++) {
14+
jewel[i] = Integer.parseInt(br.readLine());
15+
max = Math.max(max, jewel[i]);
16+
}
17+
Arrays.sort(jewel);
18+
int answer = max;
19+
int low = 1;
20+
int high = max;
21+
while (low <= high) {
22+
int mid = (low + high) / 2;
23+
int count = 0;
24+
for (int j = 0; j < M; j++) {
25+
count += jewel[j] / mid;
26+
if (jewel[j] % mid != 0) {
27+
count++;
28+
}
29+
}
30+
if (count > N) {
31+
low = mid + 1;
32+
} else {
33+
answer = Math.min(answer, mid);
34+
high = mid - 1;
35+
}
36+
}
37+
System.out.println(answer);
38+
}
39+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static final int MAX = 1299710;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
List<Integer> primeNumber = getPrimeNumber();
11+
int T = Integer.parseInt(br.readLine());
12+
for (int i = 0; i < T; i++) {
13+
int num = Integer.parseInt(br.readLine());
14+
if (num == 1) {
15+
System.out.println('0');
16+
continue;
17+
}
18+
binarySearch(primeNumber, num);
19+
}
20+
}
21+
22+
public static void binarySearch(List<Integer> primeNumber, int num) {
23+
int left = 0;
24+
int right = primeNumber.size() - 1;
25+
while (left <= right) {
26+
int mid = (left + right) / 2;
27+
int primeNum = primeNumber.get(mid);
28+
if (num <= primeNum) {
29+
right = mid - 1;
30+
if (num == primeNum) {
31+
System.out.println('0');
32+
return;
33+
}
34+
} else {
35+
left = mid + 1;
36+
}
37+
}
38+
int answer = primeNumber.get(left) - primeNumber.get(left - 1);
39+
System.out.println(answer);
40+
}
41+
42+
public static List<Integer> getPrimeNumber() {
43+
boolean[] check = new boolean[MAX];
44+
List<Integer> primeNumber = new ArrayList<>();
45+
for (int i = 2; i < MAX; i++) {
46+
if (check[i]) {
47+
continue;
48+
}
49+
primeNumber.add(i);
50+
for (int j = i; j < MAX; j += i) {
51+
if (!check[j]) {
52+
check[j] = true;
53+
}
54+
}
55+
}
56+
return primeNumber;
57+
}
58+
}

0 commit comments

Comments
 (0)