백준 1654 c++

magicdrill·2024년 2월 29일

백준 문제풀이

목록 보기
62/673

백준 1654 c++

#include <iostream>
#include <algorithm>
#include <climits>

using namespace std;

unsigned int arr[10001];
unsigned int K, N;
unsigned int max_factor = 0;

int input(int lower, int upper)
{
	//cout << "input()" << endl;
	int A;

	while (1)
	{
		cin >> A;
		if (A >= lower && A <= upper)
		{
			break;
		}
		else
		{
			;
		}
	}

	return A;
}

void input_arr()
{
	unsigned int i;

	for (i = 0; i < K; i++)
	{
		arr[i] = input(1, INT_MAX);
		max_factor = max(max_factor, arr[i]);
	}

	return;
}

unsigned int find_result()
{
	unsigned int result = 0;
	unsigned int left, mid, right;
	unsigned int sum = 0;
	unsigned int i;

	left = 1;
	right = max_factor;

	while (left <= right)
	{
		mid = (left + right) / 2;
		sum = 0;
		for (i = 0; i < K; i++)
		{
			sum += arr[i] / mid;
		}
		if (sum >= N)
		{
			left = mid + 1;
			result = max(result, mid);
		}
		else
		{
			right = mid - 1;
		}
	}

	return result;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	K = input(1, 10000);
	N = input(1, 1000000);
	input_arr();
	cout << find_result() << "\n";

	return 0;
}

0개의 댓글