๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

2512๋ฒˆ: ์˜ˆ์‚ฐ

์ฒซ์งธ ์ค„์—๋Š” ์ง€๋ฐฉ์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ •์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 3 ์ด์ƒ 10,000 ์ดํ•˜์ด๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” ๊ฐ ์ง€๋ฐฉ์˜ ์˜ˆ์‚ฐ์š”์ฒญ์„ ํ‘œํ˜„ํ•˜๋Š” N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ์ด ๊ฐ’๋“ค์€ ๋ชจ๋‘ 1 ์ด์ƒ

www.acmicpc.net

 

๐Ÿง‘‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ์ƒ‰์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

  1. ์ž…๋ ฅ ๋ฐ›์€ ์˜ˆ์‚ฐ๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  2. ์˜ˆ์‚ฐ๋“ค์˜ ํ•ฉ๊ณ„๊ฐ€ M ์ดํ•˜๋ผ๋ฉด, ์˜ˆ์‚ฐ๋“ค ์ค‘ ์ตœ๋Œ“๊ฐ’(๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’)์„ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
  3. ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด, ์ตœ์†Œ ์˜ˆ์‚ฐ๊ฐ’(1)๊ณผ ์ตœ๋Œ€ ์˜ˆ์‚ฐ๊ฐ’(๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’)์„ ๊ฐ€์ง€๊ณ  ์ด๋ถ„ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
    • "mid = (left + right) / 2" ๊ฐ’์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ์‚ฐ์˜ ์ด ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋˜, mid ๊ฐ’๋ณด๋‹ค ํฐ ์˜ˆ์‚ฐ์€ mid ๊ฐ’์œผ๋กœ ํ•ฉ๊ณ„์— ๋”ํ•ฉ๋‹ˆ๋‹ค.
    • ๋‹ค ๋”ํ•œ ์ด ํ•ฉ๊ณ„๊ฐ€ M ์ดํ•˜๋ผ๋ฉด, left = mid + 1์„ ํ•˜๊ณ  max(mid, ์ด์ „ mid)๋กœ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
    • ์•„๋‹ˆ๋ผ๋ฉด, right = mid - 1์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • left > right๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#define FAST_IO cout.tie(NULL); cin.tie(NULL); ios::sync_with_stdio(false);
using namespace std;
using uint32 = unsigned int;

int main()
{
	FAST_IO

	int N;
	cin >> N;

	vector<uint32> budgets(N);
	for (int i = 0; i < N; i++)
		cin >> budgets[i];

	sort(budgets.begin(), budgets.end());
	uint32 start = 1, last = budgets.back();
	
	uint32 M;
	cin >> M;

	if (accumulate(budgets.begin(), budgets.end(), 0) <= M)
	{
		cout << budgets.back();
		return 0;
	}


	uint32 answer = 0;
	while (start <= last)
	{
		uint32 mid = (start + last) / 2;
		uint32 money = 0;

		for (const auto& budget : budgets)
			money += (budget <= mid) ? budget : mid;

		if (money <= M)
		{
			start = mid + 1;
			answer = max(answer, mid);
		}
		else
			last = mid - 1;
	}

	cout << answer;
	return 0;
}

728x90
๋ฐ˜์‘ํ˜•