首先很难想到用二分,其次判断时要用long long....
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 10e5+10;
const int inf = 10e9+10;
int a[maxn];
bool judge(int mid, int n, int k) {
long long minute = 0;
for (int i = 0; i < n; i++) {
if (a[i] > mid) {
minute += (a[i] - mid + k - 2) / (k - 1);
}
}
return minute <= mid;
}
int main(int argc, char const *argv[]) {
int n, k;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
scanf("%d", &k);
if (k == 1) {
printf("%d\n", *max_element(a, a + n));
return 0;
}
int left = 0;
int right = inf;
while (right - left > 1) {
int mid = (left + right) / 2;
if (judge(mid, n, k)) {
right = mid;
} else {
left = mid;
}
}
printf("%d\n", right);
return 0;
}