[백준] 16564 - 히오스 프로그래머

2025. 1. 11. 03:05·PS

 

 

이분탐색 알고리즘 문제!

 

이분탐색 문제를 풀 떄 가장 먼저 찾아야 하는 조건은, 범위를 특정할 수 있는 최댓값과 최솟값이다.

문제의 조건에 따라

3 10
10
20
15

 

입력에서 10 이하의 수를 분배하였을 때 나올 수 있는 가장 높은 값은 30, 낮은 값은 10이 된다.

따라서 이진탐색 함수에서

min = levels[0] (정렬 기준)

max= levels[N-1] + K 로 설정하여 구할 수 있다.

 

 

 public static int binarySearch(int[] arr, int target, int min, int max) {
        int result = 0;
        while (min <= max) {
            int mid = (min + max) / 2;
            long count = 0;
            for (int i : arr) {
                if (mid - i > 0) {
                    count += mid - i;
                }
            }
            if (count > target) {
                max = mid - 1;
            } else {
                result = mid;
                min = mid + 1;
            }
        }
        return result;
    }

 

목표 레벨을 mid 값으로 설정한 후, 목표 레벨이 되기 위해 더해야 하는 레벨 수를 count 변수에 저장한다.

count 변수보다 target 값이 큰 경우 max 값을 목표레벨 - 1 로 설정한다.

count 변수보다 target 값이 작거나 같은 경우 mid 값을 result로 저장하고, min 값을 목표 레벨 + 1 로 설정한다.

 

 

 

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");

        int K = Integer.parseInt(input[1]);
        int N = Integer.parseInt(input[0]);
        int[] levels = new int[N];
        for (int i = 0; i < levels.length; i++) {
            levels[i] = Integer.parseInt(br.readLine());
        }

        Arrays.sort(levels);
        System.out.println(binarySearch(levels, K, levels[0], levels[N - 1] + K));

    }


    public static int binarySearch(int[] arr, int target, int min, int max) {
        int result = 0;
        while (min <= max) {
            int mid = (min + max) / 2;
            long count = 0;
            for (int i : arr) {
                if (mid - i > 0) {
                    count += mid - i;
                }
            }
            if (count > target) {
                max = mid - 1;
            } else {
                result = mid;
                min = mid + 1;
            }
        }
        return result;
    }

}

 

전체 코드

 

 

 

 

 

'PS' 카테고리의 다른 글

[백준] 2504번 - 괄호의 값  (1) 2025.01.21
[백준] 2751번 - 수 정렬하기 2 (시간 초과)  (1) 2025.01.12
[프로그래머스] lv2. 가장 큰 수  (1) 2025.01.04
[java/백준] 1764번 듣보잡  (1) 2024.02.26
[java/백준] 1316번- 그룹 단어 체커  (1) 2023.08.31
'PS' 카테고리의 다른 글
  • [백준] 2504번 - 괄호의 값
  • [백준] 2751번 - 수 정렬하기 2 (시간 초과)
  • [프로그래머스] lv2. 가장 큰 수
  • [java/백준] 1764번 듣보잡
zioni
zioni
  • zioni
    jiwon's dev.log
    zioni
  • 전체
    오늘
    어제
    • 분류 전체보기 (76)
      • spring & java (13)
      • Algorithm (14)
      • PS (37)
      • project (3)
      • experience (1)
      • etc (6)
      • study (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • Github
  • 공지사항

  • 인기 글

  • 태그

    java
    자바
    백준2525
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
zioni
[백준] 16564 - 히오스 프로그래머
상단으로

티스토리툴바