슈콩

[BOJ] 백준 16401 과자 나눠주기 본문

Algorithms/Baekjoon

[BOJ] 백준 16401 과자 나눠주기

shukong 2025. 9. 18. 23:45

[문제]

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

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Main {
	static int m,n;
	static int[] snack;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		m = Integer.parseInt(st.nextToken());
		n = Integer.parseInt(st.nextToken());
		snack = new int[n];
		st = new StringTokenizer(br.readLine());
		int max = 0;
		for(int i=0;i<n;i++) {
			snack[i] = Integer.parseInt(st.nextToken());
			if(max < snack[i]) max = snack[i];
		}
		int start = 1;
		int end = max;
		while(start<=end) {
			int mid = (start+end) / 2;
			if(can(mid)) {
				start = mid + 1; 
			}
			else
				end = mid - 1;
		}
		System.out.println(end);
	}
	private static boolean can(int mid) {
		int cnt = 0;
		for(int i=0;i<n;i++) {
			cnt += snack[i] / mid;
		}
		return cnt >= m;
	}
}

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

[BOJ] 백준 14502 연구소  (2) 2025.09.19
[BOJ] 백준 14499 주사위 굴리기  (0) 2025.09.19
[BOJ] 백준 14921 용액 합성하기  (0) 2025.09.18
[BOJ] 백준 13460 구슬 탈출 2  (2) 2025.09.18
[BOJ] 백준 1477 휴게소 세우기  (0) 2025.09.18