題目:給你一串連續的數字,連續的切割成m段,求每段和中最大的值的最小值。
分析:貪心+二分。二分上限,可以證明每次可以盡量多的加和,只要不超過上限。
說明:╮(╯▽╰)╭。
#include <cstring>
#include <cstdio>
int items[1111];
int container_size(int size, int value)
{
int sum = 0, count = 1;
for (int i = 0; i < size; ++ i) {
if (items[i] > value) {
return size;
}
if (sum + items[i] > value) {
sum = items[i];
count ++;
}else {
sum += items[i];
}
}
return count;
}
int main()
{
int n, m;
while (~scanf("%d%d",&n,&m)) {
int sum = 0, max = 0;
for (int i = 0; i < n; ++ i) {
scanf("%d",&items[i]);
sum += items[i];
if (max < items[i]) {
max = items[i];
}
}
int l = max, r = sum, mid = 0;
while (l < r) {
mid = (l+r)/2;
if (container_size(n, mid) <= m) {
r = mid;
}else {
l = mid+1;
}
}
printf("%d\n",l);
}
return 0;
}