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
|
while (l < r) { int mid = (l + r) / 2; if (a[mid] >= x) r = mid; else l = mid + 1; }
while (l < r) { int mid = (l + r + 1) / 2; if (a[mid] <= x) l = mid; else r = mid - 1; }
while (l + eps < r) { double mid = (l + r) / 2; if (calc(mid)) r = mid; else l = mid; }
for (int i = 0; i < 100; i++) { double mid = (l + r) / 2; if (calc(mid)) r = mid; else l = mid; }
bool valid(int size) { int group = 1, rest = size; for (int i = 1; i <= n; i++) { if (rest >= a[i]) rest -= a[i]; else group++, rest = size - a[i]; } return group <= m; }
int l = 0, r = sum_of_Ai; while (l < r) { int mid = (l + r) / 2; if (valid(mid)) r = mid; else l = mid + 1; } cout << l << endl;
|