题目大意:输入m和k,要把m个正整数划分成k个连续的非空序列,要求全部划分中的最大值尽量小
解题思路:用二分法找每个划分序列的最大值,使之不能再减少即可
#include <iostream>
#include <cstring>
using namespace std;
int cou, cut;
long long int a[1000];
int c[1000];
bool jud(int m) {
memset(c, 0, sizeof(c));
long long int ad = 0;
int p = 0;
int ju = 1;
for(int i = cou-1; i >= 0; i--) {
if(p == cut) {
ju = 0;
break;
}
ad += a[i];
if(ad > m) {
ad = 0;
c[i] = 1;
i++;
p++;
}
}
if(ju == 1) {
for(int i = 0; i < cou; i++) {
if(p == cut - 1)
break;
if(!c[i]) {
c[i] = 1;
p++;
}
}
return true;
}
return false;
}
void bsearch(long long int fro, long long int end) {
memset(c, 0, sizeof(c));
int m;
while(fro < end) {
m = fro + (end - fro)/2;
if(jud(m)) {
end = m;
}
else {
fro = m + 1;
}
}
jud(end);
}
int main() {
int n;
long long int all;
cin >> n;
while(n--) {
all = 0;
cin >> cou >> cut;
for(int i = 0; i < cou; i++) {
cin >> a[i];
all+=a[i];
}
bsearch(0, all);
for(int i = 0; i < cou; i++) {
cout << a[i];
if(i != cou-1) {
if(c[i] == 1) {
cout << " / ";
}
else
cout << " ";
}
else {
cout << endl;
}
}
}
return 0;
}