lan less Picking apples
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
http://acm.uestc.edu.cn/#/problem/show/1255
Description
There are N apple trees in the yard of LAN , and every autumn the tree will produce an Fi Apple.
When Apple matures, LAN will run to pick apples.
Lan the way to pick apples is very peculiar, each time you can choose a maximum of M apples and pick it off.
But 221 of the apples taken off are not from the same tree, ask LAN how many times to pick the minimum, so that each Apple will be picked off?
Input
Enter a number in the first lineNAnd m(1≤m≤N≤6 ), representing the number of apple trees, and LAN less each time to pick how many.
Second line inputNNumber, sectionINumberfi ( 0 ≤fi≤10 6 ) represents the total number of apples on this tree.
Output
Outputs a number indicating the minimum number of choices
Sample Input
5 33 2 3 2 4
Sample Output
5
HINT
Test instructions
Exercises
Exercises
Assuming that LAN can fetch M apples every time (less than m), then this number is obviously ti = (Sigma (Fi)-1)/M + 1
As for each day, only one fruit of this tree can be selected at most each time, then at least Max (Fi) should be taken
Now, make GI = max (max (Fi), Ti)
Now it proves that it can be done in GI time.
We now convert the model to n-width 1, the length of the GI, the color of I rectangle.
Each rectangle is split into a GI 1*1 rectangle, filled into a m*gi rectangle (can not be filled), satisfies the GI line, each row does not have the same color rectangle
We start with the first column, the first color, and fill in the next column whenever the column fills up (that is, when it reaches GI height), and if the color is used up, replace it with a color.
It is now proven that this ensures that each row will not have the same color.
For each color I, because of the Gi>=max (Fi) >=fi, then any one of the colors will appear in the adjacent two columns at most.
If it appears only in one column, it is obviously on a different line.
If there are two adjacent columns, then one column is filled to the top, and the following starts at the bottom. Set a column filled with AI, the following is populated with Bi, obviously when and only when the Ai+bi>gi will appear in the same row, but there is Ai+bi=fi<=max (Fi) <=gi, so it will not appear on the same line.
So we proved that LAN less can choose all the fruits in the GI times.
So the answer is Max (Ti, Max (Fi)), Time complexity O (N)
Code:
#include <cstdio>#include<algorithm>Long Longsum;intMX;intMain () {intN, M; scanf ("%d%d", &n, &M); for(inti =0; i < N; ++i) {intF; scanf ("%d", &f); Sum+=F; MX=Std::max (MX, f); } printf ("%lld\n", Std::max (sum + M-1)/M, 1LL *mx)); return 0;}
Cdoj 1255 lan less pick apples greedy