Poj1505 & uva714 copying books (DP)

Source: Internet
Author: User

Copying books
Time limit:3000 Ms
Memory limit:10000 K
Total submissions:7109
Accepted:2221

Description

Before the copyright 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 books and actors needed more copies of them, of course. so they hired then 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 shoshould 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

Source

Central Europe 1998


Q. K. I personally copied M. The minimum time is to divide M.

D [I] [J] indicates that the time required by J books before I Personal completion involves the transition equation d [I] [J] = min (d [I] [J], max (d [I-1] [K], s [J]-s [k]) k represents all the numbers between I-1 and J. S [k] represents the time and initial D from the first book to the K book [1] [I] = s [I];

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 550, INF = 0x3f3f3f3f;int a[N], flag[N], s[N],  d[N][N], t, cas, n, m, ans;int dp (int i, int j){    if (d[i][j] > 0) return d[i][j];    d[i][j] = INF;    for (int k = i - 1; k < j; ++k)        d[i][j] = min (d[i][j], max (dp (i - 1, k), s[j] - s[k]));    return d[i][j];}int main(){    scanf ("%d", &cas);    while (cas--)    {        scanf ("%d%d", &n, &m);        memset (d, -1, sizeof (d));  memset (flag, -1, sizeof (flag));        for (int i = 1; i <= n; ++i)        { scanf ("%d", &a[i]); d[1][i] = s[i] = s[i - 1] + a[i]; }        ans = dp (m, n);        for (int i = n,t=0 ; i >= 1; --i)            if ( ( (t = t + a[i]) > ans) || m > i)                {  t = a[i]; flag[i] = 0; --m; }        for (int i = 1; i <= n; ++i)        {            printf (flag[i] ? "%d" : "%d /", a[i]);            printf (i == n ? "\n" : " ");        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.