본문 바로가기
  • Let's study
PS/백준

[C++] 백준 1654번: 랜선 자르기

by 코딩고수이고파 2020. 11. 22.
#include<iostream>
#include<algorithm>

using namespace std;

long long length[10001];
long long high = 0;

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

	int k, n;
	cin >> k >> n;
	for (int i = 0; i < k; i++) {
		cin >> length[i];
		high < length[i] ? high = length[i] : high;
	}
	long long low = 1;
	long long result = 0;

	while (low <= high) {
		long long count=0;
		long long mid = (high + low) / 2;
		for (int i = 0; i < k; i++) {
			count += length[i] / mid;
		}

		if (count < n)
			high = mid - 1;
		else {
			if (result < mid)
				result = mid;
			low = mid + 1;
		}
	}
	cout << result;
}

댓글