알고리즘/문제풀이
[백준] 16401. 과자 나눠주기
BSHwan
2020. 6. 8. 11:41
반응형
https://www.acmicpc.net/problem/16401
16401번: 과자 나눠주기
첫째 줄에 조카의 수 M (1 ≤ M ≤ 1,000,000), 과자의 수 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에 과자 N개의 길이 L1, L2, ..., LN이 공백으로 구분되어 주어진다. 과자의 길이는 (1 ≤ L1, L2, ..., LN
www.acmicpc.net
import java.util.Arrays;
import java.util.Scanner;
// 16401 과자 나눠주기
public class Main {
static int M, N, result;
static int[] arr;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
M = sc.nextInt();
N = sc.nextInt();
arr = new int[N];
for(int i=0; i<N; i++) arr[i] = sc.nextInt();
Arrays.sort(arr);
bs(1, arr[N-1]);
System.out.println(result);
}
public static void bs(int s, int e) {
if(s > e) return;
int mid = (s+e)/2;
int cnt = 0;
for(int i=0; i<N; i++) cnt += arr[i]/mid;
if(cnt >= M) {
if(result < mid) result = mid;
bs(mid+1, e);
} else {
bs(s, mid-1);
}
}
}
반응형