[알고리즘] 백준 1300 - K번째 수

홍예주·2022년 3월 24일
0

알고리즘

목록 보기
59/92

1. 문제

세준이는 크기가 N×N인 배열 A를 만들었다. 배열에 들어있는 수 A[i][j] = i×j 이다. 이 수를 일차원 배열 B에 넣으면 B의 크기는 N×N이 된다. B를 오름차순 정렬했을 때, B[k]를 구해보자.

배열 A와 B의 인덱스는 1부터 시작한다.

2. 입력

첫째 줄에 배열의 크기 N이 주어진다. N은 105보다 작거나 같은 자연수이다. 둘째 줄에 k가 주어진다. k는 min(109, N2)보다 작거나 같은 자연수이다.

3. 풀이

이분탐색으로 풀 수 있다.

4. 코드


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static long n,k;
    public static long[] arr;

    public static void main(String args[]) throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(bf.readLine());
        k = Integer.parseInt(bf.readLine());

        long left = 1;
        long right = k;

        while(left<right){
            long mid =(left+right)/2;
            long count =0;

            for(int i=1;i<=n;i++){
                count+=Math.min(mid/i,n);
            }

            if(k<=count){
                right = mid;
            }
            else{
                left = mid+1;
            }
        }

        System.out.println(left);
    }



}
profile
기록용.

0개의 댓글