Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.
Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have m books (numbered 1, 2 ... m) that may have different number of pages (p1, p2 ... pm) and you want to make one copy of each of them. Your task is to divide these books among k scribes, k <= m. Each book can be assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers 0 = b0 < b1 < b2, ... < bk-1 <= bk = m such that i-th scriber gets a sequence of books with numbers between bi-1+1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the first line, there are two integers m and k, 1 <= k <= m <= 500. At the second line, there are integers p1, p2, ... pm separated by spaces. All these values are positive and less than 10000000.
Output
For each case, print exactly one line. The line must contain the input succession p1, p2, ... pm divided into exactly k parts such that the maximum sum of a single part should be as small as possible. Use the slash character ('/') to separate the parts. There must be exactly one space character between any two successive numbers and between the number and the slash.
If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.
Sample
Input | Output |
---|---|
2 9 3 100 200 300 400 500 600 700 800 900 5 4 100 100 100 100 100 | 100 200 300 400 500 / 600 700 / 800 900 100 / 100 / 100 / 100 100 |
题意: 给定n个数的序列,求将其划分为k块的方案,使各块加和最大值最小。如果有多种方案,输出越靠前的块加和越小的那个。
分析: 这道题目算是比较明显的二分了,可以先二分求出这个最小的最大值,check(x)函数返回是否可以划分出k个和小于等于x的块,check的具体实现如下,先遍历一遍数组,尽量把一个块装进尽可能多的数,如果装不下就换下一个块,如果最终块数小于等于k,那就说明可以划分成k个和小于等于x的块,如果最终块数大于k,那要划分为k个块就需要尽可能扩大每个块中元素个数,但由于每个块已经包含了最多的元素无法再扩大了,所以此时就不能划分为k个和小于等于x的块。
写好check函数就能求出这个最小的最大值了,接下来就是输出具体的划分方案。由于要越靠后的块加和越大,所以输出方案时可以从后往前枚举,再重复一遍check中的过程,这样得到的方案就是越靠后的块越大的方案了。另外这样得到的方案其各块和的最大值一定是之前二分出来的那个值,这里可以反证,假如不是的话那二分时的值就会是更小的值了。
最后在实现代码时还出了个bug,在下面代码的第59行,非常具有教育意义!
具体代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#define int long long
using namespace std;
int T, n, k, a[505];
bool check(int x){//返回能否划分为k段,且每段和都小于等于x
int sum = 0, cnt = 1;
for(int i = 1; i <= n; i++){
if(a[i] > x) return false;
if(sum+a[i] <= x){
sum += a[i];
}
else{
sum = a[i];
cnt++;
if(cnt > k)
return false;
}
}
return true;
}
signed main()
{
cin >> T;
while(T--){
scanf("%lld%lld", &n, &k);
for(int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
int l = 1, r = 1e10+10, ans = -1;
while(l <= r){
int m = l+r>>1;
if(check(m))
ans = m, r = m-1;
else
l = m+1;
}
vector<int> v[505];
int cnt = 1, sum = 0;
for(int i = n; i >= 1; i--){
if(sum+a[i] <= ans && i > k-cnt){
v[cnt].push_back(a[i]);
sum += a[i];
}
else{
v[++cnt].push_back(a[i]);
sum = a[i];
}
}
for(int i = cnt; i >= 1; i--){
printf("%lld", v[i][v[i].size()-1]);
//v[i].size()-2是个32位无符号数,而j是个64位带符号数
//所以需要先0扩展至64位然后转为带符号数
for(int j = (int)v[i].size()-2; j >= 0; j--){
printf(" %lld", v[i][j]);
}
if(i != 1) printf(" / ");
}
puts("");
}
return 0;
}