-
Notifications
You must be signed in to change notification settings - Fork 0
/
[백준 2805 c++ VVO] 나무 자르기.cpp
73 lines (68 loc) · 1.98 KB
/
[백준 2805 c++ VVO] 나무 자르기.cpp
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
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <algorithm> // fill_n, min,max, swap
//#include <map> // 중복 x
//#include <string> // getline
//#include <cstring> // memset, strok, strstr
#include <vector>
//#include <queue> // priority_queue<자료형, 구현체, 비교 연산자> (중복허용),
//#include <set> // 트리, 중복 x , multiset
//#include <unordered_set>
//#include <cmath>
using namespace std; // lower_bound, upper_bound
/*
[백준 2805 c++ VVO] 나무 자르기
문제:
접근1: 최댓값 -> 완탐/그리디/dp/이진탐색 -> 완전탐색 -> 20억 이어서 시간초과
접근2: 최댓값 -> 완탐/그리디/dp/이진탐색 -> 이진탐색 직접구현-> 자르는 길이 low=1, high=최대길이, 하고
시간복잡도: O(log2(m))
풀이: 이분탐색-파라메트릭서치
//1.입력
//2.이분탐색-파라메트릭서치: 높이를 mid로 탐색한다
//중간값
//자른 나무의 합 구하기
//1)자른 나무의 합 >= m
//2)자른 나무의 합 < m
//3.출력: 최대 높이
*/
int n, m,num;
vector<int> v;
long long sub = 0;
long long maxH = -1;
long long cut(int mid) {
long long sum = 0;
for (int i = 0; i < n; i++) {
sub = v[i] - mid;
if (sub >= 0) sum += sub;
}
return sum;
}
void binary(int start, int end,int tar) {
while (start <= end) {
long long mid = (start + end) / 2;
// 자른 나무의 합 구하기
long long sum = cut(mid);
if (sum == tar) {
maxH = max(maxH, mid);
return;
}
else if (sum > tar) {
start = mid + 1;
maxH = max(maxH, mid);
}
else end = mid - 1;
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(0);
//1.입력
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> num;
v.push_back(num);
}
//2.이진탐색: 자를 톱의 높이를 탐색 0~1000000000
binary(0, 1000000000,m);
cout << maxH << '\n';
return 0;
}