BOJ : https://www.acmicpc.net/problem/2110
github : https://github.com/junho0956/Algorithm/blob/master/2110/2110/%EC%86%8C%EC%8A%A4.cpp
C개의 공유기를 N개의 집에 남김없이 설치해야하는 문제이다.
** 설치가 가능할 때 집과 집 사이의 거리중 가장 인접한(작은) 거리를 최대로 만드는 문제이다. **
코드와 함께 이분탐색의 조건을 같이 주석처리 해놓았다.
더보기
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
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, c, x;
vector<int> v;
cin >> n >> c;
for (int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
int left = 1, right = v[n - 1] - v[0];
int result_len = 0;
while (left <= right) {
int len = (left + right) / 2;
int cnt = 1, last_house = 0;
int min_len = INT_MAX;
bool check = false;
for (int i = 1; i < n; i++) {
if (v[i] - v[last_house] >= len) {
min_len = min(min_len, v[i] - v[last_house]);
cnt++, last_house = i;
if (cnt == c) {
check = true; break;
}
}
}
// 공유기 c개를 설치할 수 있다면 -> 현재 len 보다 더 큰 값을
if (check) {
result_len = max(result_len, min_len);
left = len + 1;
}
// 공유기 c개를 설치할 수 없다면
else right = len - 1;
}
cout << result_len;
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'algorithm > BOJ' 카테고리의 다른 글
BOJ 10816번 숫자 카드2 (0) | 2020.01.16 |
---|---|
BOJ 10815번 숫자 카드 (0) | 2020.01.16 |
BOJ 2805번 나무 자르기 (0) | 2020.01.16 |
BOJ 1654번 랜선 자르기 (0) | 2020.01.16 |
BOJ 1167번 트리의 지름 (0) | 2020.01.15 |