Application search - calculate the optimal solution: Aizu - ALDS1_4_D Allocation

Application search - calculation of the optimal solution

topic:

You are given nn packages of wiwi kg from a belt conveyor in order (i=0,1,...n−1i=0,1,...n−1). You should load all packages onto kk trucks which have the common maximum load PP. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load PP.

Write a program which reads nn, kk and wiwi, and reports the minimum value of the maximum load PP to load all packages from the belt conveyor.

Input

In the first line, two integers nn and kk are given separated by a space character. In the following nn lines, wiwi are given respectively.

Output

Print the minimum value of PP in a line.

Constraints

  • 1≤n≤100,0001≤n≤100,000
  • 1≤k≤100,0001≤k≤100,000
  • 1≤wi≤10,0001≤wi≤10,000

Sample Input 1

5 3

8

1

7

3

9

Sample Output 1

10

If the first truck loads two packages of {8,1}{8,1}, the second truck loads two packages of {7,3}{7,3} and the third truck loads a package of {9}{9}, then the minimum value of the maximum load PP shall be 10.

 

Sample Input 2

4 2

1

2

2

6

Sample Output 2

6

If the first truck loads three packages of {1,2,2}{1,2,2} and the second truck loads a package of {6}{6}, then the minimum value of the maximum load PP shall be 6.

Ideas:

In fact, the purpose of this question is very simple idea, is the need to look at the meaning of the title, and be careful TLE . It is: as long as the carrying capacity of the truck did not meet P , we let it in the order of goods shipped, and finally in the calculation of all truck carrying amount of the sum can be. Here in P as argument, to write a number of returned goods can be loaded v function v = F ( P ). This function of the complexity of the algorithm is O ( n- ). Then just call this function, use " P increases, v increases" (strictly speaking, P increased v will not reduce) the nature of the search request with the dichotomy P . At this time complexity of the algorithm is O ( nlogP ).

 

Code:

 

 1 #include <iostream>
 2 
 3 using namespace std;
 4 #define MAX 100000
 5 typedef long long llong;
 6 int n,k;
 7 llong T[MAX];
 8 
 9 int check(llong P)
10 {
11     int i=0;
12     for(int j=0;j<k;j++)
13     {
14         llong s=0;
15         while(s+T[i]<=P)
16         {
17             s+=T[i];
18             i++;
19             if(i==n) return n;
20         }
21     }
22     return i;
23 }
24 
25 int solve()
26 {
27     llong left=0;
28     llong right=100000*10000;
29     llong mid;
30     while(right-left>1)
31     {
32         mid=(right+left)/2;
33         int v=check(mid);
34         if(v>=n) right=mid;
35         else
36             left=mid;
37     }
38     return right;
39 }
40 
41 int main()
42 {
43     cin>>n>>k;
44     for(int i=0;i<n;i++)
45     {
46         cin>>T[i];
47     }
 48      Llong years = solve ();
49      cout << age << endl;
50      return  0 ;
51 }

 

 

to sum up:

A topic that is in turn sent the goods on the conveyor belt, meaning that, as long as the belt sent the goods, must be loaded into carts. The first time I saw when the subject is not aware of this order, so always do not understand the idea of ​​the book, wasted a lot of time.

Guess you like

Origin www.cnblogs.com/lavena/p/10988910.html