아주 전형적인 이분탐색으로 정답찾기 쉬워유
* functional.h 사용 X
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 300000
int n, m, res = 1000000000;
int jewerly[MAX];
int max(int first, int second) {
if (first > second) return first;
else return second;
}
int min(int first, int second) {
if (first > second) return second;
else return first;
}
bool solve(int mid) {
int person = 0;
for (int i = 0; i < m; i++) {
person += jewerly[i] / mid;
if (jewerly[i] % mid) {
person += 1;
}
}
if (person > n) return false;
else return true;
}
int main() {
int i = 0, r;
scanf("%d %d", &n, &m);
int left = 0, right = 0;
for (int i = 0; i < m; i++) {
scanf("%d", &jewerly[i]);
right = max(right, jewerly[i]);
}
while (left <= right) {
int mid = (left + right) / 2;
if (solve(mid)) {
res = min(res, mid);
right = mid - 1;
}
else {
left = mid + 1;
}
}
printf("%d\n", res);
}
|
cs |