https://www.acmicpc.net/problem/2512
기준 예산과 비교하면 된다.
이분탐색할때 종료지점을 제대로 두자
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
vector<int> budget(n);
for (int i = 0; i < n; i++)
{
cin >> budget[i];
}
int plan;
cin >> plan;
int bottom = 0, top = *max_element(budget.begin(), budget.end());
int mid_val;
int ans = 0;
// 가능한 최대 상한값을 찾기 위해 등호추가가
while (bottom <= top)
{
int tmp = 0;
mid_val = (top + bottom) / 2;
for (int i = 0; i < n; i++)
{
tmp += (mid_val > budget[i]) ? budget[i] : mid_val;
}
if (tmp <= plan)
{
ans = mid_val;
bottom = mid_val + 1;
// cout << "bottom : " << bottom << endl;
}
else if (tmp > plan)
{
top = mid_val - 1;
// cout << "top : " << top << endl;
}
}
cout << ans;
}
728x90
'백준이당' 카테고리의 다른 글
[C++] 백준 2608번 : 로마 숫자 (0) | 2025.05.19 |
---|---|
[C++] 백준 11687번 : 팩토리얼 0의 개수 (0) | 2025.05.14 |
[C++] 백준 7562번 : 나이트의 이동 (0) | 2025.04.16 |
[C++] 백준 1325번 : 효율적인 해킹 (0) | 2025.04.16 |
[C++] 백준 24480번 : 알고리즘수업 - 깊이 우선 탐색2 (0) | 2025.04.16 |