Skip to content

Commit c4f3886

Browse files
authored
[BOJ] 2792 보석 상자 (S1)
1 parent c503644 commit c4f3886

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

박예진/11주차/260309.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//https://www.acmicpc.net/problem/2792
2+
#include <iostream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <string>
6+
using namespace std;
7+
8+
int N, M; // 아이들 수, 색상 수
9+
int arr[300001], res = 1e9;
10+
11+
void binary_search() {
12+
int maxColor = 0;
13+
for (int i = 0; i < M; i++) {
14+
maxColor = max(maxColor, arr[i]);
15+
}
16+
17+
int left = 1;
18+
int right = maxColor; // 색상중 가장 큰 값
19+
20+
while (left <= right) {
21+
int mid = (left + right) / 2; // 한 아이가 받는 최대 보석의 수
22+
23+
long long num = 0; // 그렇게 나눴을 때 필요한 아이들 수
24+
for (int i = 0; i < M; i++) {
25+
num += arr[i] / mid;
26+
if (arr[i] % mid) num += 1;
27+
}
28+
29+
if (num <= N) {
30+
right = mid - 1;
31+
res = min(res, mid);
32+
}
33+
else {
34+
left = mid + 1;
35+
}
36+
}
37+
}
38+
39+
int main() {
40+
ios::sync_with_stdio(false);
41+
cin.tie(NULL); cout.tie(NULL);
42+
43+
cin >> N >> M;
44+
for (int i = 0; i < M; i++) {
45+
cin >> arr[i];
46+
}
47+
binary_search();
48+
cout << res;
49+
50+
return 0;
51+
}

0 commit comments

Comments
 (0)