파라메트릭 서치 ㅇㅅㅇ
오늘의 교훈 : unsigned 를 함부로 쓰지말자
-5 > 0 이 값이 true 로 나온다
또하나 그냥 long long 쓰자 ^^
전체 코드
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
ll N, M, input;
vector<ll> treeHeight;
ll getTree(ll height) {
ll res = 0;
for (int i=0; i<N; i++) {
ll t = treeHeight[i] - height;
if (t > 0) {
res += t;
}
}
return res;
}
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll start, end, mid, res = 0;
cin >> N >> M;
for (int i=0; i<N; i++) {
cin >> input;
treeHeight.push_back(input);
}
sort(treeHeight.begin(), treeHeight.end());
start = 0;
end = treeHeight[N-1];
while (start <= end) {
mid = (start + end) / 2;
if (getTree(mid) >= M) {
res = mid;
start = mid + 1;
}
else {
end = mid - 1;
}
}
cout << res << "\n";
return 0;
}
'ALGORITHM > OTHER' 카테고리의 다른 글
[c++] 1655 가운데를 말해요 (힙!) (2) | 2021.02.08 |
---|---|
[c++] 11054 가장 긴 바이토닉 부분 수열 (이분탐색) (0) | 2021.02.03 |
[c++] 1654 랜선 자르기 (파라메트릭 서치(Parametric Search)) (0) | 2021.01.28 |
[c++] 14425 문자열 집합 - Trie (트라이), set, unordered_set (0) | 2021.01.24 |
2231 분해합, 7568 덩치, 1018 체스판 다시칠하기, 1436 영화감독 숌 (0) | 2021.01.10 |