原题:
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 ) that may have different number of pages () and you want to make one copy of each of them. Your task is to divide these books among k scribes, . 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 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.
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, . At the second line, there are integers separated by spaces. All these values are positive and less than 10000000.
For each case, print exactly one line. The line must contain the input succession 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.
2 9 3 100 200 300 400 500 600 700 800 900 5 4 100 100 100 100 100
100 200 300 400 500 / 600 700 / 800 900 100 / 100 / 100 / 100 100
题目大意:
要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的。每个抄写员的速度是相同的,求所有书抄完所用的最少时间的分配方案。
分析与总结:
所化的总时间取决于所有抄写员中任务最多的那个,是经典的最大值最小化问题。LRJ《算法入门经典》P151页有介绍:
/** * 最大值最小化问题: 二分法。 刘汝佳那本白书上也说的很详细。 * 先把最大值的可能区间计算出来,也就是[0, maxAns] maxAns 就是所有页数总和。 * 然后再在答案区间里先把“最大值最小化”。 * 得到最小化后的最大值以后,就可以根据这个最大值来对最终答案进行计算。 * 如何分区,也就是计算最后答案ans: 这里就要用到贪心思想,因为题目要求 * 如果有多种可能情况,让区间集合所含的元素数从小到大排列。这样就可以贪心地 * 从所有pages的最后一个元素往前遍历来进行划分区间,只要其总和不大于之前所求的最大值, * 就归到那个区间。 * 在把所有区间分好后,还有个情况就是,这个时候可能需要3个斜杠来分4个区间, * 然而只用了2个(比如样例2),这样还有一个斜杠没用到,就再用贪心,把pages从第一个开始往后扫, * 如果当前pages不用划斜杠,而剩余斜杠数大于0,则在这里添个斜杠。 */ #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <algorithm> #define INF 0x7fffffff #define MAXS 501 #define LL long long using namespace std; int m, k; LL maxAns; int pages[MAXS], ans[MAXS]; bool judge(LL x) { int sum = 0, t = k; for(int i = 0; i < m; i ++) { sum += pages[i]; if(sum > x) { i --; sum = 0; t --; } if(!t) { if(i != m - 1) return false; else return true; } } return true; } void solve() { memset(ans, 0, sizeof(ans)); LL l, r, cur; l = 0; r = maxAns; while(l < r) { cur = (l + r) / 2; if(judge(cur)) r = cur; else l = cur + 1; } int sum = 0; for(int i = m - 1; i >= 0; i --) { sum += pages[i]; if(sum > r) { sum = 0; ans[++ i] = 1; k --; } } int i = 1; while(k > 1) { for(; i < m; i ++ ) { if(!ans[i]) { ans[i] = 1; k --; break; } } } printf("%d", pages[0]); for(int i = 1; i < m; i ++) { if(ans[i]) printf(" /"); printf(" %d", pages[i]); } printf("\n"); } int main() { int t; scanf("%d", &t); while(t --) { maxAns = 0; scanf("%d%d", &m, &k); for(int i = 0; i < m; i ++) { scanf("%d", &pages[i]); maxAns += pages[i]; } solve(); } return 0; }