Skip to content

Commit a54ff82

Browse files
committed
[BOJ]K번째 수/골드1/1시간 10분
1 parent b12948b commit a54ff82

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//BOJ1300 K번째 수, 골드1
2+
//이 문제 또한 처음에 이분 탐색을 생각하지 못했다.
3+
//N 크기 조건을 생각하지 않고 냅다 배열을 사용하였지만 역시 메모리 초과가 발생하였다.
4+
//N은 10^5 로 전체 행렬의 크기는 최대 10^10 으로, 10,000,000,000 (100억)이 된다.
5+
//이 말은 행렬을 만들어 브루트 포스로 탐색하기에는 너무 많은 메모리와 시간을 잡아먹게 된다는 의미다.
6+
// 참고 자료 : https://st-lab.tistory.com/281
7+
8+
import java.io.*;
9+
import java.util.*;
10+
11+
public class BOJ1300 {
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
15+
int N = Integer.parseInt(br.readLine());
16+
int K = Integer.parseInt(br.readLine());
17+
18+
long start = 1;
19+
long end = K; //여기서 틀림
20+
21+
while(start <= end) {
22+
long mid = (start+end) / 2;
23+
long count = 0;
24+
25+
// 임의의 x(mid)에 대해 i번 째 행을 나눔으로써 x보다 작거나 같은 원소의 개수
26+
// 누적 합을 구한다.
27+
// 이 때 각 행의 원소의 개수가 N(열 개수)를 초과하지 않는 선에서 합해주어야 한다.
28+
for(int i = 1; i <= N; i++) {
29+
count += Math.min(mid/i, N);
30+
}
31+
32+
if(count < K) {
33+
start = mid + 1;
34+
}else {
35+
end = mid - 1;
36+
}
37+
}
38+
39+
bw.write(start+"\n");
40+
bw.flush();
41+
bw.close();
42+
br.close();
43+
}
44+
}

0 commit comments

Comments
 (0)