### Copying Books Statement

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 (`p _{1}, p_{2} … p_{m}`) 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 = b`such that

_{0}< b_{1}< b_{2}, … < b_{k-1}<= b_{k}= m`i`-th scriber gets a sequence of books with numbers between

`b`and

_{i-1}+1`b`. 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.

_{i}#### Input

The input consists of `N` cases (equal to about 200). 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 `p _{1}, p_{2}, … p_{m}` 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 `p _{1}, p_{2}, … p_{m}` 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.

#### Example

0 1 2 3 4 5 6 7 8 9 10 11 |
<b>Sample input:</b> 2 9 3 100 200 300 400 500 600 700 800 900 5 4 100 100 100 100 100 <b>Sample output:</b> 100 200 300 400 500 / 600 700 / 800 900 100 / 100 / 100 / 100 100 |

### Copying Books Solution

**Try to think this problem with binary search, you can fix LTE message!**

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
using namespace std; int N, M; int pages[500]; bool ok(long long x) { int next = 0; int i; for (i = 0; i < M; i++) { long long sum = 0; do { sum += pages[next]; if (sum <= x) next++; } while (next < N && sum <= x); if (next == N && sum <= x) return true; } return false; } int main() { # ifndef ONLINE_JUDGE freopen("books.in", "r", stdin); freopen("books.out", "w", stdout);# endif int t, i, j; scanf("%d", & t); int books[500]; int work[500][500]; while (t--) { scanf("%d %d", & N, & M); memset(books, 0, sizeof(books)); long long total = 0; int _m = 0; for (i = 0; i < N; i++) { scanf("%d", pages + i); total += pages[i]; _m = max(_m, pages[i]); } long long l = max((total + M - 1) / M, (long long) _m); long long u = total; while (u > l) { long long m = (l + u) / 2; if (ok(m)) u = m; else l = m + 1; } int next = N - 1; for (i = M - 1; i >= 0; i--) { long long sum = 0; for (;;) { sum += pages[next]; if (sum <= l && next >= i) work[i][books[i]++] = pages[next--]; else break; } } for (i = 0; i < M - 1; i++) { for (j = books[i] - 1; j >= 0; j--) printf("%d ", work[i][j]); printf("/ "); } for (i = books[M - 1] - 1; i; i--) printf("%d ", work[M - 1][i]); printf("%d\n", work[M - 1][0]); } } |

See more SPOJ Solution here: https://free4dev.com/category/spoj-solution

Or search google with keyword: free4dev + “spoj_problem_id”.