반응형
    
    
    
  -문제

-코드
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> a;
int calcul(int n)
{
	int total = 0;
	for (int i = 0; i < a.size(); i++)
	{
		total += min(a[i], n);
	}
	return total;
}
int main() 
{
	int n, m, tmp, total=0, res, max = 0;
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &tmp);
		a.push_back(tmp);
		total += a[i];
		if (max < a[i]) max = a[i];
	}
	scanf("%d", &m);
	if (total <= m)
	{
		printf("%d", max);
		return 0;
	}
	
	int rt=max, lt=1, mid;
	
	while (lt<=rt)
	{
		mid = (rt + lt) / 2;
		if (calcul(mid) <= m)
		{
			lt = mid + 1;
			res = mid;
		}
		else
		{
			rt = mid - 1;
		}
	}
	printf("%d", res);
	return 0;
}
반응형
    
    
    
  '이론 > 코딩테스트' 카테고리의 다른 글
| [백준] 2110번 공유기 설치 (이분 탐색) (0) | 2022.08.25 | 
|---|---|
| [백준] 16401번 과자 나눠주기 (이분 탐색) (0) | 2022.08.25 | 
| [백준] 10816번 숫자카드2 (이분 탐색) (0) | 2022.08.25 | 
| [백준] 2776번 암기왕 (이분탐색) (0) | 2022.08.25 | 
| [백준] 1590번 캠프가는 영식 (그리디 알고리즘) (0) | 2022.08.25 | 
 
                    
                   
                    
                   
                    
                  