Fourth week of training (efficient algorithm design) Question B (two-point search optimization problem)

Source: Internet
Author: User

---restore content starts---

Description

Before the invention of book-printing, it is very hard-make a copy of a book. All of the contents had to is re-written by hand and 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 its name was Xaverius endricus Remius Ontius Xendrianus ( xerox). Anyway, the work is very annoying and boring. And the only-to-speed it up is to hire more scribers.


Once upon a time, there was a theater ensemble the 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 havemBooks (numbered) the May has different number of pages () and want to make one copy of each of the them. Your task is to divide these books amongkScribes,. Each book can is assigned to a scriber only, and every scriber must get a continuous sequence of books. That is means, there exists an increasing succession of numbers such thatI-th Scriber gets a sequence of books with numbers betweenbI-1+1 andbI. The time needed to make a copy of the "All the books are determined by the" the Scriber who were 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 first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly-lines. At the first line, there is integers m and K,. At the second line, there is integers separated by spaces. All these values is 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 the maximum sum of a single PA RT should be as small as possible. Use the slash character ('/') to separate the parts. There must is exactly one space character between any of the successive numbers and between the number and the slash.


If there is more than one solution, print the one of the minimizes the work assigned to the first Scriber and then the Secon D Scriber etc. But each scriber must is 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

This problem should first think good ideas, this problem is actually a limit value of the process, such as a permutation, I divided a lot of intervals, require each interval can not be greater than a certain value, set this value is M, then this value should be the smallest, take example 1 for example, the minimum should be 900, in Example 2 for example, should be 100, Why? Because you may have a certain value in the arrangement as an interval, if your limit is less than 900, then 900 will not be able to enter any interval, the division will be a problem

We can also conclude that the M maximum value should be the sum of the permutations (the entire arrangement is divided into one interval).

Now that we've got a range of limits, what's the limit value? You can use the limit as the basis for how many intervals, I can enumerate from the smallest point of the limit to the maximum, until the limit is just enough to divide the interval into k (note there may be a precision problem). However, although the enumeration method can find the value of the demand, but ...

Time Out, huh?

Although the length of the array is only 500, what is the concept of the maximum value of the array element up to 10 million and 10 million times 500? This enumerates the time complexity of (n^2) *m, which time out:

The best way is to optimize the binary search, with the limit to judge, if the limit value so that the interval division number is greater than k, then the limit value must be larger, to the right, and vice versa to the left to check

This time complexity is M*N*LOGN, time performance greatly improved ...

#include"Iostream"#include"Algorithm"#include"CString"#include"Cstdio"UsingNamespace Std;int nK;int a[510];LongLong tot;int Maxa;voidInit(){cin>>n>>k; tot=0; Maxa=-1;For(int I=0; I<n; I++){cin>>a[I]; Tot+=a[I]; Maxa=Max(A[I],maxa);}Sort (a,a+n);}IntNeed(LongLong num){LongLong C=0;int sum=1;For(int I=0; I<n; I++)If(c+a[I]<=num) c+=a[I];Else{C=a[I];sum++;}return sum;}voidPrint(LongLong num){int last[510];LongLong done=0;Memset(Last,0,sizeof(Last));int remain= k;For(int I= N-1; I>=0; I--){If(Done+ A[I]> num|| I+1< remain){Last[I]=1; Remain--; Done= A[I];}Else{Done+ = A[I];}}For(int I=0; I< n-1; I++){Printf("%d"+ F[I]);If(Last[I])Printf("/ ");}Printf("%d\ n"+ F[n-1]);}voidGuess(){LongLong LRM; L=maxa; r=tot;While(l<r){m=l+ (R-L)/2;If(Need(M) <=k) r=m;else L=m+1;}print (L} int main () { Span class= "Sh-type" >int t,t Cin>> T=twhile (T--) { init (); guess ();  return 0;< Span class= "Sh-cbracket" >             

Fourth week of training (high efficiency algorithm design) Question B (two-point search optimization problem)

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.