Algorithm/Baekjoon
[Algorithm] Baekjoon 2110번 : 공유기 설치
MaxLevel
2023. 10. 20. 13:32
https://www.acmicpc.net/problem/2110
2110번: 공유기 설치
첫째 줄에 집의 개수 N (2 ≤ N ≤ 200,000)과 공유기의 개수 C (2 ≤ C ≤ N)이 하나 이상의 빈 칸을 사이에 두고 주어진다. 둘째 줄부터 N개의 줄에는 집의 좌표를 나타내는 xi (0 ≤ xi ≤ 1,000,000,000)가
www.acmicpc.net
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
vector<long long> positions;
int n, c;
bool check(int mid)
{
int count = c - 1;
int prev = positions[0];
for (int i = 1; i < positions.size(); ++i)
{
if (positions[i] - prev >= mid)
{
--count;
prev = positions[i];
}
if (count == 0) return true;
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> c;
for (int i = 0; i < n; ++i)
{
int input;
cin >> input;
positions.push_back(input);
}
sort(positions.begin(), positions.end());
int left = 1;
int right = positions[n-1] - positions[0];
int answer = 0;
while (left <= right)
{
int mid = (left + right) / 2;
if (check(mid))
{
left = mid + 1;
answer = mid;
}
else
{
right = mid - 1;
}
}
cout << answer;
}
|
cs |
어렵지 않은 이분탐색 문제.
최소거리값을 이분탐색으로 결정해 놓고, 그 최소거리를 만족시키면서 주어진 공유기개수를 전부 소모할 수 있는지를 검사하면 된다.
거리값은 최대 10억이지만 이분탐색으로 진행 시 log 1000000000 이라해봤자 30번 될까말까다.
거기에 매번 집개수만큼 하니 log 1000000000 * 200,000이 시간복잡도가 되시겠다.