UVa 714:copying Books, maximum minimization problem (greedy + two points)

Source: Internet
Author: User
Tags printf

Before the invention of book-printing, it is very hard to make a copy of the book. All of the contents had to is re-written by hand from so called scribers. The scriber had been given a book and then several months he finished it copy. One of the most famous Scribers lived in the 15th century and his name is Xaverius endricus remius Ontius Xendrianus ( xerox). Anyway, the work was very annoying and boring. And the only way to speed it up is to hire more scribers.

Once upon a time, there is a theater ensemble that wanted-play famous antique. The scripts of plays were divided into many books and actors needed-more copies of them, of course. So they hired many scribers to make copies of this. Imagine you havemBooks (numbered) could have different number of pages () and you want to make one copy of the them. Your task is to divide this books amongkScribes,. Each book can is assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists a increasing succession of numbers such thatI-th Scriber gets a sequence of books with numbers betweenb I-1+1 andbI. The time needed to make a copy of the "All" is determined by the Scriber who was assigned the most work. Therefore, our goal are 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 ' the ' input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the "the", there are two integers m and K. At the second line, there are integers separated by spaces. All of these values are positive and less than 10000000.

Output

For each case, print exactly one line. The line must contain the input succession divided to exactly K parts such that's maximum sum of a single par T should be as small as possible. Use the slash character ('/") to separate the parts. There must be exactly one spaces character between any two successive numbers and between the number and the slash.

If there is more than one solution, print the one of that minimizes the work assigned to the The one Scriber D Scriber etc. But Each scriber must is assigned at least one book.

Sample Input

2
9 3 MB
900
5 4
100 100 100 100 100

Sample Output

500/600 700/800 900
100/100/100/100 100

Given a sequence of n numbers. The requirement is divided into M sequential subsequence. A partitioning method is required to make the. The maximum value of the and of the subsequence. is the smallest.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Train of thought: LRJ The maximum value of the book to minimize the problem. Turn the problem into the ability to divide the input sequence into m sequences so that all and no more than X. If it is false. Just look up. If it's true, look down. At first you can set the boundary as the smallest number in the sequence. The sum of the upper bound sequence. Then make a binary lookup.

The way to judge division in the process is to use greed. Try to divide it to the right.

This problem in the output place toss me for a while--the eggs are broken. Note that if the remaining digits are equal to the number of '/', then each of the remaining digits is inserted directly between the '/'.

There's one more point of attention. You should remember to use Longlong when you are two points.

Code:

#include <stdio.h> #include <string.h> int t;
int n, m;
Long long start, end;
int num[505];
int out[505];
int outn;
    int judge (int x) {//judge whether the input sequence can be divided into M sequential subsequence.
    int sb = n-1;
	for (int i = 0; i < m i + +) {long long sum = 0;
	    while (sum + NUM[SB] <= x) {sum + = NUM[SB];
	    if (sb = = 0) return 1;
	SB--;
} return 0;
    int main () {scanf ("%d", &t);
	while (T-) {start = 10000000;
	outn = 0;
	memset (out, 0, sizeof (out));
	scanf ("%d%d", &n, &m);
	    for (int i = 0; i < n; i + +) {scanf ("%d", &num[i]);
	    End + = Num[i];
	if (Start > Num[i]) start = Num[i];
	Long long sb;//remember long long or it would be a tragedy.
	    while (Start < end) {//two points find SB = (start + end)/2; 
		if (judge (SB)) {if (end = SB) break;
	    end = SB;
		else {if (start = SB) break;
	    start = SB;
	} if (!judge (SB)) SB + +;
	int i = n-1;
	    while (I!=-1) {//with an out to determine the position of the output '/'.
Long long sum = 0;	    while (sum + num[i] <= sb) {sum + = num[i-];
	    if (i = = 1) break;
		} if (I!=-1) {out[i] = 1;
	    Outn + +; } if (Outn!= m-1) {//if '/' not sufficient.
		    Fill the previous number with '/' for (int i = 0; i < n; i + +) {if (!out[i]) {out[i] = 1;
		    Outn + +;
		if (outn = = m-1) break;
	    for (int i = 0; i < n-1 i + +) {printf ("%d", num[i]);
	if (Out[i]) printf ("/");
    printf ("%d\n", num[n-1]);
return 0; }

Author: csdn Blog shuangde800

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.