uva 714 Copying Books (二分)

uva 714 Copying Books

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 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, 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 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

2 9 3 100 200 300 400 500 600 700 800 900 5 4 100 100 100 100 100

Sample Output

100 200 300 400 500 / 600 700 / 800 900 100 / 100 / 100 / 100 100

题目大意:有N个数字,将其分成M段,,要求在N个数字中加入M-1个分隔符,使得每一段的数的和 中的最大值最小。解题思路:最大值的最小化用二分。先用二分法找到,最小的最大值,然后通过这个最大值去找到分隔符的位置。ll;;ll n, m, num[505], rec[505], sum;int check(ll x) {ll s = 0;int cnt = 0;for (int i = 0; i < n; i++) {s += num[i];if (num[i] > x) return 0;if (s > x) {s = num[i];cnt++;}}if (cnt >= m) {return 0;}return 1;}int main() {int T;sum = 0;scanf(“%d”, &T);while (T–) {memset(rec, 0, sizeof(rec));scanf(“%lld %lld”, &n, &m);for (int i = 0; i < n; i++) {scanf(“%lld”, &num[i]);sum += num[i];}ll l, r, mid;l = 0, r = sum;while (l < r) {mid = (l + r) / 2;if (!check(mid)) l = mid + 1;else r = mid;}sum = 0;int cnt2 = 0;for (int i = n – 1; i >= 0; i–) {sum += num[i];if (sum > l || m – cnt2 – 1 == i + 1) {sum = num[i];rec[i] = 1;cnt2++;}}printf(“%lld”, num[0]);if (rec[0]) printf(” /”);for (int i = 1 ; i < n; i++) {printf(” %lld”, num[i]);if (rec[i]) printf(” /”);}printf(“\n”);}return 0;}

而开始追寻他内心世界的真正财富

uva 714 Copying Books (二分)

相关文章:

你感兴趣的文章:

标签云: