슈콩

[BOJ] 백준 2805 나무 자르기 본문

Algorithms/Baekjoon

[BOJ] 백준 2805 나무 자르기

shukong 2025. 10. 6. 12:02

[문제]

https://www.acmicpc.net/problem/2805

 

 

[소스 코드]

import java.io.*;
import java.util.*;

public class Main {
	static int n;
	static long m;
	static int[] arr;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		n = Integer.parseInt(st.nextToken());
		m = Long.parseLong(st.nextToken());
		st = new StringTokenizer(br.readLine());
		arr = new int[n];
		for(int i=0;i<n;i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}
		Arrays.sort(arr);
		long start = 0;
		long end = arr[arr.length-1];
		while(start<=end) {
			long mid = (start + end) / 2;
			if(possible(mid)) 
				start = mid + 1;
			else
				end = mid - 1;
		}
		System.out.println(end);
	}
	private static boolean possible(long val) {
		long cnt = 0;
		for(int i=0;i<n;i++) {
			if(arr[i]>val) cnt += arr[i] - val; 
		}
		if(cnt<m) return false;
		return true;
	}
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 21921 블로그  (0) 2025.11.11
[BOJ] 백준 3151 합이 0  (0) 2025.10.06
[BOJ] 백준 9935 문자열 폭발  (0) 2025.10.04
[BOJ] 백준 28215 대피소  (0) 2025.10.04
[BOJ] 백준 13458 시험 감독  (0) 2025.10.02