백준
백준 소스코드 [C++] 2805 나무자르기
Hani_Levenshtein
2021. 7. 5. 09:24
https://www.acmicpc.net/problem/2805
2805번: 나무 자르기
첫째 줄에 나무의 수 N과 상근이가 집으로 가져가려고 하는 나무의 길이 M이 주어진다. (1 ≤ N ≤ 1,000,000, 1 ≤ M ≤ 2,000,000,000) 둘째 줄에는 나무의 높이가 주어진다. 나무의 높이의 합은 항상 M보
www.acmicpc.net
백준 소스코드 [C++] 2805 나무자르기
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string.h>
#include <set>
#include <map>
#include <unordered_map>
#include <sstream>
#include <cstdlib>
#include <cassert>
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define pli pair<ll, int>
#define make_unique(v) sort(all(v)), v.erase(unique(all(v)), v.end())
typedef long long ll;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
int MIN = 0, MAX = 0, maxHeight = 0;
ll sum;
cin >> n >> m;
vector<int> tree(n);
for (int i = 0;i < n;i++) {
cin >> tree[i];
MAX = max(MAX, tree[i]);
}
while (MIN <= MAX) {
int MID = (MIN + MAX) / 2;
sum = 0LL;
for (int i = 0;i < n;i++)
sum += max(0, tree[i] - MID);
if (sum >= m) {
maxHeight = max(maxHeight, MID);
MIN = MID + 1;
}
else MAX = MID - 1;
}
cout << maxHeight;
return 0;
}