1148 G. Balanced Distribution

1148 G. Balanced Distribution

The meaning of problems:
a given length of \ (n-\) loop sequence \ (a_0, \ cdots, a_ {n-1} \) and the constant \ (K \) , to ensure that each operation sequence is an integer of average value of the ring. continuous \ (K \) number and re-allocation values at the same premise. It requires at least the same number of operations so that all the values, and outputs the program.

answer:

  1. Consider the case where the line segment. If the leftmost \ (K \) number greater than \ ((. 1-K) \ A bar \) , may be pre \ ((k-1) \ ) number assigned to \ ( \ a bar \) , the remaining values are assigned to the first \ (K \) number, the right and then recursively process the remaining \ ((NK) \) number. otherwise, the right to process \ ((NK) \ ) number, and so on to \ (K \) number sufficient to complete before \ (K \) assigned number. in this case, the same first consider the leftmost part of this \ (K \) number and whether the allocation is completed.
    implementations may use a recursive function distribute(l,len,need)expressed processed \ (L \) as a starting point, \ (len \) is the interval length, and requires additional leftmost \ (need \) values in external distribute(0,n,0)manner call. details reference code
    can be found, reduces the length of each \ (. 1-K \) , and when the length is exactly \ (K \) when only treated once, and therefore need to operate \ (\ lceil \ dfrac { {K}. 1-n--}. 1 \ rceil \) .
  2. If the interval \ ([x, y] \ ) satisfies \ (\ DisplayStyle \ sum_ {I} = X ^ ya_i = (X-Y +. 1) \ A bar \) , then this section can be a separate processing section. This condition can also be written \ (\ {prefix_sum} text _y-Y \ bar A = {prefix_sum} \ text _ {X-. 1} - (X-. 1) \ bar A \) . Thus can be all satisfied \ (\ text { prefix_sum} _i-i \ bar a \) is equal to a position as a breakpoint (the \ (I \) and \ (i + 1 \) OFF).
  3. Suppose there are two adjacent interval lengths are \ (X \) and \ (Y \) , separate operation requires \ (\ lceil \ dfrac {x -1} {k-1} \ rceil + \ lceil \ dfrac {y-1 } {K-. 1} \ rceil \) , merge operation requires \ (\ lceil \ dfrac {X + Y-. 1} {K-. 1} \ rceil \) , note that if and only if \ (x \ equiv y \ \ pmod {k-1} \ ) equiv1 time, the number of merge operation will be less. Thus, the ring should be broken down into several mold \ (k-1 \) I section 1, as direct or ring segment of the entire operation .
  4. Length die \ (k-1 \) I 1 is equivalent to two molding breakpoint location \ (k-1 \) in the sense of the difference \ (1 \) , and therefore in each \ (\ text {prefix_sum} _i- i \ bar a \) is equal to a position such that they find some of the breakpoint position mode \ (k-1 \) is a continuously increasing (in the sense of each die \ (1 + \) ) sequence. Because it is ring, considering times as long sequence positions (latter half times longer requires \ (n-+ \) ). recently obtained from the back sweep of the next legal position of each breakpoint (i.e. the molding position \ (k-1 \) the sense current position \ (+ 1 \) ), then legally in the method multiplied \ (2 ^ i \) positions. then from each position of the front half of times longer, determines whether possible several steps went to the corresponding position of the latter half times the length (i.e., from \ (I \) through several steps to the \ (n-I + \) ). from these legitimate position selected from a number of steps as the longest answer. Finally, the first step of the allocation for each section according to the breakpoints.
    Code

Guess you like

Origin www.cnblogs.com/Heltion/p/12329221.html