문제
https://www.acmicpc.net/problem/6236
해설
인출 횟수가 n보다 작기만 하면 되는 문제
이분 탐색을 이용
코드
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
49
50
51
52
|
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int n,m,cost,mx;
ll ret, low,hi,mid;
vector<int> v;
bool go(int num){
ll tmp=1,tmp_cost = 0;
for(int i : v){
if((tmp_cost + i)<=num){
tmp_cost+= i;
}
else{
tmp_cost = i;
tmp++;
}
}
return tmp <= m;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> n >> m;
for(int i=0;i<n;i++){
cin >> cost;
mx = max(mx,cost);
hi += cost;
v.push_back(cost);
}
low = mx;
while(low<=hi){
mid = (low+hi)/2;
if(go(mid)){
hi = mid-1;
ret = mid;
}
else{
low = mid+1;
}
}
cout << ret;
return 0;
}
|
cs |
'백준 알고리즘(C++)' 카테고리의 다른 글
백준 1269번 대칭 차집합 ( C++ ) (0) | 2024.09.10 |
---|---|
백준 7795번 먹을 것인가 먹힐 것인가 ( C++ ) (0) | 2024.09.10 |
백준 2343번 기타 레슨 ( C++ ) (0) | 2024.09.10 |
백준 2792번 보석 상자 ( C++ ) (0) | 2024.09.09 |
백준 3190번 뱀 ( C++ ) (2) | 2024.09.08 |