Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 유니온 파인드
- 정렬
- ue5
- 누적 합
- 스택
- BFS
- VR
- 재귀
- 투 포인터
- 백준
- 유니티
- Unreal Engine 5
- 다이나믹 프로그래밍
- 시뮬레이션
- c++
- 문자열
- 구현
- XR Interaction Toolkit
- 백트래킹
- 다익스트라
- 수학
- 그리디 알고리즘
- 그래프
- 알고리즘
- 트리
- 우선순위 큐
- DFS
- 브루트포스
- 자료구조
- Team Fortress 2
Archives
- Today
- Total
1일1알
백준 2110번 공유기 설치 C++ 본문
우선 입력받은 집의 위치들을 오름차순으로 정렬한다.
가능한 최소 간격은 1, 최대 간격은 제일 오른쪽 집-제일 왼쪽 집 이다.
left를 1, right를 house[n-1] - house[0] 으로 놓고 이분탐색을 하면서 답을 구할 수 있다.
설치한 공유기의 수가 C보다 크거나 같으면 간격을 넓혀야 하기 때문에 left를 mid+1로 바꾼 뒤 답을 갱신해주고,
설치한 공유기의 수가 C보다 작다면 간격을 좁혀야 하기 때문에 right를 mid-1로 바꾼다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
int n, c;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> c;
vector<int> house(n);
for (int i = 0; i < n; i++) {
cin >> house[i];
}
sort(house.begin(), house.end());
int left = 1;
int right = house[n - 1] - house[0];
int ans = -1;
while (left <= right) {
int mid = (left + right) / 2;
int lastIndex = 0;
int cnt = 1;
for (int i = 1; i < n; i++) {
if (house[i] - house[lastIndex] >= mid) {
cnt++;
lastIndex = i;
}
}
if (cnt >= c) {
ans = max(ans, mid);
left = mid + 1;
}
else {
right = mid - 1;
}
}
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 2011번 암호코드 C++ (0) | 2021.12.15 |
---|---|
백준 1325번 효율적인 해킹 C++ (0) | 2021.12.14 |
백준 2343번 기타 레슨 C++ (0) | 2021.12.12 |
백준 1743번 음식물 피하기 C++ (0) | 2021.12.11 |
백준 2302 극장 좌석 C++ (0) | 2021.12.10 |