Editorial for IOI '04 P5 - Farmer


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Let gi be a field or a strip. Denote by ni the number of cypresses in a field or a strip. If we denote by ei the number of olive trees in a gi, we have: ei=ni if gi is a field or ei=ni1 if gi is a strip.

Consider now the following KNAPSACK problem: maxi=1n+meixi, such that i=1n+mnixiQ and xi{0,1}, where n,m are the numbers of fields and strips respectively.

An optimum solution xi, 1in+m, of this problem consists of a subset of gi such that the total number of their cypresses is at most Q and the total number of the included olive trees is maximized. However in general Q=i=1n+mnixi<Q. If this is the case, then there is some gi such that xi=0 and ni>QQ, for otherwise the optimum solution can be improved by the inclusion of gi, a contradiction. Therefore, adding a chain of QQ cypresses of gi and its QQ1 olive trees to the optimum solution of the knapsack problem above, yields an optimum solution. The KNAPSACK problem can be solved optimally in O((n+m)Q) time by a Dynamic Programming algorithm.


Comments

There are no comments at the moment.