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

2110๋ฒˆ: ๊ณต์œ ๊ธฐ ์„ค์น˜

์ฒซ์งธ ์ค„์— ์ง‘์˜ ๊ฐœ์ˆ˜ N (2 ≤ N ≤ 200,000)๊ณผ ๊ณต์œ ๊ธฐ์˜ ๊ฐœ์ˆ˜ C (2 ≤ C ≤ N)์ด ํ•˜๋‚˜ ์ด์ƒ์˜ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์ง‘์˜ ์ขŒํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” xi (0 ≤ xi ≤ 1,000,000,000)๊ฐ€

www.acmicpc.net

 

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

๋“ฑ๊ฐ„๊ฒฉ์œผ๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ๊ฐ„๊ฒฉ์„ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ์ƒ‰์œผ๋กœ ๊ตฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ๋ฐ›์€ ์ง‘ ์œ„์น˜๋“ค์„ ๋ฐฐ์—ด(arr)์— ๋‹ด๊ณ , ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค€ ๋’ค์— ๋‹ค์Œ ๊ณผ์ •์„ ์ง„ํ–‰ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

  1. left = 0 (์ฒซ ๋ฒˆ์งธ ~ ์ฒซ ๋ฒˆ์งธ ๊ณต์œ ๊ธฐ ์‚ฌ์ด์˜ ๋ฒ”์œ„), right = arr[N - 1] (์ฒซ ๋ฒˆ์งธ ~ ๋งˆ์ง€๋ง‰ ๊ณต์œ ๊ธฐ ์‚ฌ์ด์˜ ๋ฒ”์œ„)
  2. mid = (left + right) / 2๋กœ ์ค‘๊ฐ„ ๋ฒ”์œ„๊ฐ’์„ ๊ตฌํ•˜๊ณ , ํ˜„์žฌ ์œ„์น˜๊ฐ’(currentPos) = arr[0]๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค๋‹ˆ๋‹ค.
  3. ๊ณต์œ ๊ธฐ ์„ค์น˜ ๊ฐœ์ˆ˜๋ฅผ ์…€ ๋ณ€์ˆ˜(routerCount)๋ฅผ 1๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. (๋งจ ์ฒ˜์Œ์— ํ•˜๋‚˜ ์„ค์น˜ํ•˜๊ณ  ์‹œ์ž‘ํ•˜๋ฏ€๋กœ)
  4. arr[1] ~ arr[N - 1]๊นŒ์ง€ ์ˆœํšŒํ•˜๋ฉฐ, arr[i] - currentPos >= mid๋ผ๋ฉด, routerCount++
    • ๊ทธ๋ฆฌ๊ณ  currentPos = arr[i]๋กœ ํ•˜์—ฌ ์œ„์น˜๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ค๋‹ˆ๋‹ค.
  5. 4๋ฒˆ ๋ฃจํ”„๊ฐ€ ๋๋‚œ ํ›„ routerCount < C๋ผ๋ฉด, ๊ฐ„๊ฒฉ์ด ๋„ˆ๋ฌด ๋„“๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ right = mid - 1์„ ํ•˜์—ฌ ๋‹ค์‹œ 2 ~ 4๋ฒˆ ๋ฐ˜๋ณต
  6. routerCount >= C๋ผ๋ฉด, ์ข€ ๋” ์ž‘์€ ๊ฐ„๊ฒฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ left = mid + 1์„ ํ•˜์—ฌ ๋‹ค์‹œ 2 ~ 4๋ฒˆ ๋ฐ˜๋ณต
  7. left <= right๋ฅผ ๋งŒ์กฑํ•  ๋•Œ๊นŒ์ง€ ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณต

 

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

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

int FindMaxDistance(const vector<int>& positions, const int N, const int C)
{
	int left = 0, right = positions.back() - positions.front();
	int distance = 1;

	while (left <= right)
	{
		int routerCount = 1;
		int range = (right + left) / 2;
		int currentPosition = positions.front();

		for (int i = 1; i < N; i++)
		{
			if (positions[i] - currentPosition >= range)
			{
				routerCount++;
				currentPosition = positions[i];
			}
		}
			
		if (routerCount < C)
			right = range - 1;
		else
		{
			left = range + 1;
			distance = range;
		}
	}

	return distance;
}

int main()
{
	FAST_IO
	int N, C;
	cin >> N >> C;

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

	sort(positions.begin(), positions.end());
	cout << FindMaxDistance(positions, N, C);
	return 0;
}

728x90
๋ฐ˜์‘ํ˜•