반응형
https://www.acmicpc.net/problem/6236
6236번: 용돈 관리
현우는 용돈을 효율적으로 활용하기 위해 계획을 짜기로 하였다. 현우는 앞으로 N일 동안 자신이 사용할 금액을 계산하였고, 돈을 펑펑 쓰지 않기 위해 정확히 M번만 통장에서 돈을 빼서 쓰기로
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, M;
vector<int> cost;
void Input(){
cin >> N >> M;
for(int i=0; i<N; i++){
int c;
cin >> c;
cost.push_back(c);
}
}
bool withDraw(ll k){
int cnt = 0;
ll curMoney = 0;
for(int i=0; i<N; i++){
if(k < cost[i]){
return false;
}
if(curMoney < cost[i]) {
curMoney = k;
cnt++;
}
curMoney -=cost[i];
}
return cnt <= M;
}
ll binarySearch(){
ll st = 1, en = 100000*10000;
while(st<=en){
ll mid = (st+en)/2;
if(withDraw(mid)){
en = mid - 1;
}
else{
st = mid + 1;
}
}
return st;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
Input();
cout << binarySearch() << '\n';
return 0;
}
반응형
'Algorithm > problem' 카테고리의 다른 글
백준 15685번: 드래곤 커브 - 구현 C++ (0) | 2022.07.05 |
---|---|
백준 16434번 : 드래곤 앤 던전 - 이분탐색, 구현 C++ (0) | 2022.07.04 |
백준 2343: 기타레슨 - 이분탐색(Parameter Searche) C++ (0) | 2022.07.03 |
백준 1939번: 중량제한 - Parametric Search, BFS (0) | 2022.07.03 |
백준 15990번 : 1,2,3 더하기 5 - DP (0) | 2022.07.03 |