반응형
https://www.acmicpc.net/problem/2512
2512번: 예산
첫째 줄에는 지방의 수를 의미하는 정수 N이 주어진다. N은 3 이상 10,000 이하이다. 다음 줄에는 각 지방의 예산요청을 표현하는 N개의 정수가 빈칸을 사이에 두고 주어진다. 이 값들은 모두 1 이상
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int n,m, sum, maxx;
vector<int> v;
bool decide(int c){
long long cnt = 0;
for(int i=0; i<n; i++){
if(v[i] < c){
cnt += v[i];
}
else cnt += c;
}
return cnt <= m;
}
void Input(){
cin >> n;
for(int i=0; i<n; i++){
int t;
cin >> t;
v.push_back(t);
sum+=t;
maxx = max(maxx, t);
}
cin >> m;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
Input();
if(sum<=m){
cout << maxx <<'\n';
}
else {
int lo = 1;
int hi = 1e9;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (decide(mid)) {
lo = mid + 1;
} else hi = mid - 1;
}
cout << hi << '\n';
}
return 0;
}
반응형
'Algorithm > problem' 카테고리의 다른 글
[백준] 4354번 : 문자열 제곱 - 자세한 설명 포함 KMP 알고리즘 응용 (0) | 2022.06.25 |
---|---|
백준 1786번 : 찾기 - KMP 알고리즘 (0) | 2022.06.24 |
백준 2110번 : 공유기 설치 - binary search , Decide problem - C++ (0) | 2022.06.22 |
[LeetCode] best-position-for-a-service-centre - ternary search (0) | 2022.06.22 |
[LeetCode] Split Array Largest Sum - Binary Search (0) | 2022.06.22 |