UVa714 and uva714

Source: Internet
Author: User

UVa714 and uva714

Description

 

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

 

N books, 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. If there are multiple allocation schemes, divide them as far as possible.

Solution:

1. the minimum time required depends on the person who copied the most books. Therefore, only those who copy the most books can copy as few as possible and the time is the least. Therefore, this question becomes the problem of minimizing the maximum value.

Which of the following is the minimum value of the maximum sequence? We may consider the range of the largest sequence, but it is not difficult to think that it is the min Number of pages -- sum of the whole sequence. We need to find the minimum value of its maximum sequence. Because each book has 1 <= x <= 10000000 pages, sum is definitely larger than int, And the search range is huge. In order not to time out, we can use the bipartite method... (in fact, I don't know why I used the binary method. I think of a far-fetched reason. In the book, we use the binary method ....)

2. Then we use the bipartite method to narrow down the scope until we can find the minimum value of the largest sequence. Here we use x to represent... Is it swollen? How can we find x! You can use the bipartite method to first obtain the midpoint of the x range, and then sum from sequence a [0]. If it is added to the number of I

If it is found that x is greater than the midpoint, it indicates that x is in the second half and changed to the left endpoint. Otherwise, it indicates that x is in the first half and changed to the right endpoint. Finally, find x.

3. The last step is simple. You only need to mark it and then output it ....

Here are two pieces of code. The first is the answer code, and the second is some output I added in the test... maybe it is helpful to understand ........

 

1. correct code:

 

1 # include <stdio. h> 2 # include <string. h> 3 # define LL long 4 int min = 0, m, k, a [505], ans [505]; 5 LL sum = 0; 6 bool juge (LL x) 7 {8 LL sum2 = 0; 9 int t = k; 10 for (int I = 0; I <m; I ++) {11 sum2 + = a [I]; 12 if (sum2> x) {13 t --; // records the number of strokes 14 I --; 15 sum2 = 0; 16} 17 if (! T) {// if 18 if (I! M-1) return false; // if it is finished, but not the last one, it indicates that the minimum value of the maximum sequence is greater than the current value of x ..... 19 else return true; 20} 21} 22 return true; 23} 24 25 void zhaox () 26 {27 memset (ans, 0, sizeof (ans )); 28 LL l = min, r = sum, mid; 29 while (l <r) {// when the left endpoint is equal to the right endpoint, a number of x30 mid = (r + l)/2 is determined; // The midpoint 31 if (juge (mid )) // determine which side of the vertex is 32 r = mid; 33 else34 l = mid + 1; 35} 36 LL sum3 = 0; 37 for (int I = s-1; i> = 0; I --) {38 sum3 + = a [I]; 39 if (sum3> r) {40 sum3 = 0; 41 k --; 42 ans [++ I] = 1; // mark 43} 44} 45 46 while (k> 1) {// if it is not completed, draw it next. Here, based on the meaning of the question, we need to make the front as small as possible, so draw it from the front .. (from 1, it's because of the output relationship. I guess it's not quite clear ...) 47 for (int I = 1; I <m; I ++) {48 if (! Ans [I]) {49 ans [I] = 1; // Mark 50 k --; 51 break; 52} 53} 54} 55 // print (); 56 printf ("% d", a [0]); 57 for (int I = 1; I <m; I ++) {58 if (ans [I]) printf ("/"); 59 printf ("% d", a [I]); 60} 61 printf ("\ n"); 62} 63 int main () 64 {65 int T; 66 scanf ("% d", & T); 67 while (T --) {68 scanf ("% d", & m, & k); 69 for (int I = 0; I <m; I ++) {70 scanf ("% d", & a [I]); 71 if (min> a [I]) min = a [I]; // The calculation range is 72 sum + = a [I]; // calculation range 73} 74 zhaox (); // Add the output part to the function of x. 75} 76 return 0; 77}


 

 

Lab code:

1 # include <stdio. h> 2 # include <string. h> 3 # define LL long 4 int min = 0, m, k, a [505], ans [505]; 5 LL sum = 0, j = 1; 6 bool juge (LL x) 7 {8 printf ("\ n % d times \ n", j ++); 9 LL sum2 = 0; 10 int t = k; 11 for (int I = 0; I <m; I ++) {12 sum2 + = a [I]; 13 if (sum2> x) {14 sum2 = 0; 15 t --; 16 I --; 17 printf ("% d", a [I]); 18} 19 20 if (! T) {21 if (I! = S-1) {printf ("\ n % d \ n", 0); printf ("% d \ n", a [I], a M-1]); return false;} 22 else {printf ("\ n % d \ n", 1); return true ;} 23} 24} 25 printf ("\ n % d \ n", 1); 26 return true; 27} 28 29 void zhaox () 30 {31 memset (ans, 0, sizeof (ans); 32 LL l = min, r = sum, mid; 33 while (l <r) {34 mid = (r + l)/2; 35 printf ("\ nx = % d \ n", r); 36 if (juge (mid) 37 r = mid; 38 else39 l = mid + 1; 40} 41 LL sum3 = 0; 42 for (int I = m-1; I> = 0; I --) {43 sum3 + = a [I]; 44 if (sum3> R) {45 sum3 = 0; 46 k --; 47 ans [++ I] = 1; 48} 49} 50 while (k> 1) {51 for (int I = 1; I <m; I ++) {52 if (! Ans [I]) {53 ans [I] = 1; 54 k --; 55 break; 56} 57} 58} 59 // print (); 60 printf ("% d", a [0]); 61 for (int I = 1; I <m; I ++) {62 if (ans [I]) printf ("/"); 63 printf ("% d", a [I]); 64} 65 printf ("\ n"); 66} 67 int main () 68 {69 int T; 70 scanf ("% d", & T); 71 while (T --) {72 scanf ("% d", & m, & k); 73 for (int I = 0; I <m; I ++) {74 scanf ("% d", & a [I]); 75 if (min> a [I]) min = a [I]; 76 sum + = a [I]; 77} 78 zhaox (); 79} 80 return 0; 81}

 

 

 

 

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.