Description
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
2
9 3
100 200 300 400 500 600 700 800 900
5 4
100 100 100 100 100
Sample Output
100 200 300 400 500 / 600 700 / 800 900
100 / 100 / 100 / 100 100
Source
假设有M本书(编号为1,2,…,M),想将每本复制一份,M本书的页数可能不同(分别是P1,P2,…,PM)。任务时将这M本书分给K个抄写员(K≤M〉,每本书只能分配给一个抄写员进行复制,而每个抄写员所分配到的书必须是连续顺序的。
意思是说,存在一个连续升序数列0=b0<b1<b2<…<bk-1<bk=m,这样,第i号抄写员得到的书稿是从bi-1+1到第bi本书。复制工作是同时开始进行的,并且每个抄写员复制的速度都是一样的。所以,复制完所有书稿所需时间取决于分配得到最多工作的那个抄写员的复制时间。试找一个最优分配方案,使分配给每一个抄写员的页数的最大值尽可能小(如存在多个最优方案,只输出其中一种)。
设dp[i,j]表示前j个人复制前i本书所需要的最少时间,有状态转移方程dp[i,j]=min(dp[i,j],max(dp[v,j-1],sum[v+1,i])),其中1<=i<=m,1<=j<=k,j-1<=v<=i-1,sum[v+1,j]表示第v+1本书到第i本书的页数之和。
#include<iostream>
using namespace std;
const int MAXN = 510;
int sum[MAXN],path[MAXN],dp[MAXN][MAXN];
int main(){
int m,k,i,j,v,ca,p,t;
scanf("%d",&ca);
while(ca--){
scanf("%d %d",&m,&k);
for(sum[0]=0,i=1;i<=m;i++){
scanf("%d",&p);
sum[i]=sum[i-1]+p;
}
memset(dp,-1,sizeof(dp));
for(dp[0][0]=0,i=1;i<=m;i++)
for(j=1;j<=i && j<=k;j++){
if(j==1) dp[i][j]=sum[i];
else
for(v=j-1;v<=i-1;v++){
t=max(dp[v][j-1],sum[i]-sum[v]);
if(dp[i][j]==-1 || t<=dp[i][j])
dp[i][j]=t;
}
}
for(i=m,j=k-1,p=0;i>=1;i--){
p+=sum[i]-sum[i-1];
if(p>dp[m][k] || i<=j){
path[j--]=i+1;
p=sum[i]-sum[i-1];
}
}
for(i=j=1;i<=m;i++){
if(i>1) printf(" ");
if(j<k && path[j]==i){
printf("/ ");
j++;
}
printf("%d",sum[i]-sum[i-1]);
}
printf("\n");
}
return 0;
}