题意:给定一列数,分成m段,使每段和的最大值最小。
考虑二分最小段和size,答案显然满足单调性。可以在每次check中累加数列元素判断当前组的总和是否在size以内。由于序列元素均为非负整数,前缀和数组的值满足非严格单调递增,那么可以在前缀和上再套一个二分来优化暴力累加的过程。
我不知道优化以后的复杂度怎么分析,反正它跑的快多了
代码:
- #include <iostream>
- #include <cstdio>
- #define maxn 100010
- using namespace std;
- int a[maxn], n, m;
- long long s[maxn];
- bool div(int sum) {
- int cnt = 0;
- for (int i = 1; i <= n; ) {
- if (a[i] > sum) return false;
- int l = i, r = n;
- while (l < r) {
- int mid = (l + r + 1) >> 1;
- s[mid] - s[i - 1] <= sum ? l = mid : r = mid - 1;
- }
- ++cnt, i = l + 1;
- }
- return cnt <= m;
- }
- int main() {
- // freopen("testdata-10.in", "r", stdin);
- ios::sync_with_stdio(0);
- cin >> n >> m;
- int l = 0, r = 0;
- for (int i = 1; i <= n; ++i)
- cin >> a[i], s[i] = a[i] + s[i - 1], l = max(l, a[i]);
- r = s[n];
- while (l < r) {
- long long mid = (l + r) >> 1;
- if (div(mid))
- r = mid;
- else l = mid + 1;
- }
- cout << l;
- return 0;
- }