首页 > 代码库 > poj 1505 Copying Books

poj 1505 Copying Books

http://poj.org/problem?id=1505
Copying Books
Time Limit: 3000MS Memory Limit: 10000K
Total Submissions: 7053 Accepted: 2200

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

29 3100 200 300 400 500 600 700 800 9005 4100 100 100 100 100

Sample Output

100 200 300 400 500 / 600 700 / 800 900100 / 100 / 100 / 100 100
题意:m本书,k个人,用/划分k-1个区间,怎样划分使区间最大的和最小。如果有相同的划分,就输出第一个区间最小的,如果第一区间相等,输出第二区间最小的,依次类推。
每个人都必须有一本书复制,每本书都要复制。
分析:用区间dp,dp[i][j]表示前j个人复制i本书,最大时间中最小。用path数组来记录/的位置。
dp[i][j]是枚举第j个人,前j-1个人最小dp[j-1][j-1],则第j个人就要复制sum[j,i],sum[j,i]表示第j本书到第i本书的页数的总和。
j-1个人也可以复制比j-1更多的书,用一个变量v表示前j-1的人获得的书,v可是在j-1和i-1变化,因为必须要留一本给第j个人。
则前j-1个人就是dp[v][j-1],则第j个人复制的书就是sum[v+1,i]表示第v+1本书到第i本书的页数之和。j-1<=v<=i+1,v的变化来求出第j个人最大的时间。在总体dp[i][j]中挑出最小的。
因此状态转移方程:

dp[i][j]=min(dp[i][j],max(dp[v][j-1],sum[v+1,i))
1<=i<=m,1<=j<=k&&j<=i,j-1<=v<=i+1
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int dp[505][505];int main(){    int i,t,a[505],k,m,j,v,sum[505],temp,p,path[505];    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&m,&k);           sum[0]=0;         for(i=1;i<=m;i++)          {              scanf("%d",&a[i]);              sum[i]=sum[i-1]+a[i];          }          memset(dp,0,sizeof(dp));          memset(path,0,sizeof(path));//记录的/的路径一定要清零,不然影响下一次的结果。          for(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++)                      {                                               temp=max(dp[v][j-1],sum[i]-sum[v]);                      if(dp[i][j]==0||dp[i][j]>=temp)                            dp[i][j]=temp;                      }                   }             }           }//dp[m][k]表示前k个人复制前m本书所需要的最大时间中最小.              p=0;j=k-1;//有k个人只有k-1的标记处。           for(i=m;i>=1;i--)           {              p+=sum[i]-sum[i-1];                         if(p>dp[m][k]||i==j)//p从后面求和,如果p大于dp[m][k],p就可以记录一下,因为每一段              //路径除了那个最大的区间之外相等,其余都小于dp[m][k],i==j是每本书都仅有一个人对应。              {                  path[j--]=i+1;                  p=sum[i]-sum[i-1];              }           }             v=1;         for(i=1;i<m;i++)          {              printf("%d ",a[i]);              if(i+1==path[v])                {                     printf("/ ");                   v++;                }          }         printf("%d\n",a[m]);    }    return 0;}/*29 3100 200 300 400 500 600 700 800 9005 4100 100 100 100 100*/