Link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 113 & page = show_problem & problem = 655
Original question:
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 calledScribers. 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 haveMBooks (numbered) that may have different number of pages ()
And you want to make one copy of each of them. Your task is to divide these booksKScribes,. 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 Such
ThatI-Th scriber gets a sequence of books with numbersBI-1 + 1 andBI. 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 consistsNCases. The first line of the input contains only positive integerN. Then follow the cases. Each case consists of exactly two lines. At the first line, there are
Two IntegersMAndK,. At the second line, there are integers 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 divided
Into exactlyKParts 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
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
Question:
N books to be copied, numbered 1, 2, 3... n. Each book has 1 <= x <= 10000000 pages. To assign these books to k copywriters, the numbers of the books assigned to a copywriter must be consecutive. The speed of each copywriter is the same. Find the allocation scheme of the minimum time used to copy all books.
Analysis and Summary:
The total time depends on the one with the most tasks among all the copywriters, which is the classic maximum minimization problem. On the p151 page of lrj getting started with algorithms:
In this case, it should be noted that 32-bit int may overflow during summation, so long is used.
Code:
/** Ultraviolet A: 714-copying books * Time: 0.008 S * Author: d_double **/# include <cstdio> # include <cstring> # define maxn 505 using namespace STD; int M, K; long arr [maxn], sum, Min, ans; bool vis [maxn]; inline int divide (long m) {memset (VIS, 0, sizeof (VIS); int CNT = 0; int Pos = m-1; while (Pos> = 0) {long sum = 0; bool OK = true; while (Pos> = 0 & sum + arr [POS] <= m) {OK = false; sum + = arr [POS]; -- Pos;} If (o K) {return k + 1; // return a number greater than k} If (Pos> = 0) vis [POS] = true; ++ CNT;} return CNT ;} long long binary () {long left = min, Right = sum, mid; while (left <right) {mid = (left + right)> 1; if (divide (MID) <= k) Right = mid; else left = Mid + 1;} return right;} inline void output () {int CNT = divide (ANS ); for (INT I = 0; I <M-1 & CNT <K; ++ I) if (! Vis [I]) {vis [I] = true; ++ CNT ;}for (INT I = 0; I <m; ++ I) {if (I) printf ("% LLD", arr [I]); else printf ("% LLD", arr [I]); If (vis [I]) {printf ("/") ;}} puts ("") ;}int main () {int t; scanf ("% d", & T ); while (t --) {scanf ("% d", & M, & K); sum = 0; min = 0; For (INT I = 0; I <m; ++ I) {scanf ("% LLD", & arr [I]); sum + = arr [I]; If (ARR [I]> min) min = arr [I];} ans = binary (); output ();} return 0 ;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)