[2] [medium difficulty] noip simulation competition intelligence brother's salary

Source: Internet
Author: User

Cong GE's salary

(Money/money. In/money. out)

Time limit 1000 ms memory 256 MB

Description

Lwher. With M settlement time, Cong Ge can freely arrange the time, that is to say, when to get the money, what the boss says is not, Cong GE has the right to speak!

(Because Cong Ge is a local tyrant and he is the boss of the boss, do you think the boss dares to assign tasks to cong ge? So Cong GE's job is to get the money and leave the house ...)

Cong GE does not like having too much money at a time, so he wants to arrange the time for taking the money to minimize the maximum amount of money he takes at a time. (Make sure you get the money on the last day)

Cong Ge: dizzy, is to give you a series, let you divide into M segments, so that the numbers in each segment and the maximum minimum

Input

Number of rows 2 n, m

Next n rows, each row has one number, representing VI.

Output

The minimum maximum amount of money.

Sample Input

7 5

100

400

300

100

500

101

400

Sample output

500

Example

100 400 // 300 100 500 // 101 // 400 //

"//" Indicates that Cong Ge is going to take money.

Data range

20% 1 <= n <= 20

Another 20% 1 <= n <= 50, and the sum of VI cannot exceed 1000

100% 1 <= n <= 100,000, m <= N, VI <= 10,000

Ideas

The question surface must be more domineering after my processing...

Obviously, we can see that the biggest, minimum, or minimum, is the obvious binary answer .. (It seems that Wu Tong told me that year --)

The second answer, and then the linear scan to determine whether the answer is correct... I didn't think it was too difficult to write binary for the first time.

Code

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 using namespace STD; 5 Int n, m; 6 int V [100005]; 7 int l, r, mid; 8 bool work (INT large) 9 {10 int Total = 0; // record division times 11 int sum = 0; // record the total number of current splits 12 for (INT I = 1; I <= N; I ++) 13 {14 if (V [I]> large) {return false;} 15 if (sum + V [I])> large) 16 {17 sum = V [I]; 18 total + = 1; 19 if (total> m) {return false;} 20} 21 else {sum + = V [I];} 22} 23 if (total + 1) <= m) {return true;} else {return false;} 24} 25 26 int main () 27 {28 freopen ("money. in "," r ", stdin); 29 freopen (" money. out "," W ", stdout); 30 CIN> N> m; 31 for (INT I = 1; I <= N; I ++) {CIN> V [I]; R + = V [I];} 32 L = 0; 33 while (R-l)> 1) 34 {35 mid = (L + r)> 1; 36 IF (Work (MID) = true) {r = mid;} else {L = Mid + 1 ;} 37} 38 If (Work (L) = true) {cout <L <Endl;} else {cout <r <Endl ;}39 return 0; 40}

 

The row number is displayed specially. Are you afraid of having finished 40 lines of code? (/Sneer)

Result

It took a long time ,.. But it seems that there is no way to optimize it...

 

[2] [medium difficulty] noip simulation competition intelligence brother's salary

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.