16401번: 과자 나눠주기

문제접근🤔


놓쳤던 부분😅


코드😁


5828 KB

236 ms

#include <iostream>
#include <vector>
#include <algorithm>

int m, n;
std::vector<int> snack;
int answer = 0;

void input_setting()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
}

void input()
{
	std::cin >> m >> n;
	snack.resize(n);
	for (int i = 0; i < n; i++)
		std::cin >> snack[i];
}

void solution()
{
	std::sort(snack.begin(), snack.end());
	int start = 0;
	int end = snack[n - 1];

	while (start <= end)
	{
		int count = 0;
		int mid = (start + end) / 2;
        if (mid == 0)
            return ;
		for (int i = 0; i < n; i++)
			if (snack[i] >= mid)
				count += snack[i] / mid;
		if (count >= m)
		{
			answer = mid;
			start = mid + 1;
		}
		else
			end = mid - 1;
	}
}

void print()
{
	std::cout << answer;
}

int main(void)
{
	input_setting();
	input();
	solution();
	print();
	return (0);
}