题目链接
思路
$$ 二分 $$
时间复杂度
$$ O(Nlog(max(a_i))) $$
代码
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 1e5 + 10;
int n, m;
int a[MAXN];
bool check(int mid) {
int ans = 0;
for (int i = 1; i <= n; i++) {
int x = a[i] - mid;
if (x < 0) {
x = 0;
}
if (m != 1) {
ans += x / (m - 1);
if (x % (m - 1) != 0) {
ans++;
}
} else {
if (x > 0) {
return false;
}
}
if (ans > mid) {
return false;
}
}
return true;
}
int main() {
scanf("%d", &n);// don't forget &
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);// don't forget &
}
scanf("%d", &m);// don't forget &
int l = 1, r = 1e9, ans = r;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%d", ans);
return 0;
}