#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//https://www.acmicpc.net/problem/2805

int n = 0, m = 0;
vector<int> treeHeight;

int FindMaxHeight(int start, int end) {
	int maxHeight = 0;

	while (start <= end) {
		int mid = (start + end) / 2;
		long long takeTree = 0;

		for (int a : treeHeight)
			if (a > mid)
				takeTree += a - mid;

		if (takeTree < m)
			end = mid - 1;
		else{
			maxHeight = mid;
			start = mid + 1;
		}
	}
	return maxHeight;
}

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

	cin >> n >> m;

	int num = 0;
	for (int i = 0; i < n; ++i) {
		cin >> num;
		treeHeight.push_back(num);
	}
	sort(treeHeight.begin(), treeHeight.end());

	cout << FindMaxHeight(0, treeHeight.back());

	return 0;
}

https://www.acmicpc.net/problem/2805

 

2805번: 나무 자르기

첫째 줄에 나무의 수 N과 상근이가 집으로 가져가려고 하는 나무의 길이 M이 주어진다. (1 ≤ N ≤ 1,000,000, 1 ≤ M ≤ 2,000,000,000) 둘째 줄에는 나무의 높이가 주어진다. 나무의 높이의 합은 항상 M보

www.acmicpc.net

 

'알고리즘 공부' 카테고리의 다른 글

프로그래머스 136798. 기사단원의 무기  (1) 2023.12.27
백준 10815번 숫자 카드 C++  (0) 2023.10.16
백준 10866번 덱 C++  (1) 2023.10.06
백준 10845번 큐 C++  (0) 2023.10.06
백준 10816번 숫자 카드 2 C++  (0) 2023.10.05