Editorial for COCI '15 Contest 5 #4 Poplava
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.
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's first notice that the maximal amount of water contained in a histogram with columns is exactly . The configuration to achieve this is, for example, .
The main idea of the solution is to remove several columns from the middle and put them in descending order on the side. Then the total amount of water would depend only on the columns left in the middle. A column of height in the middle would contribute water.
Let's assume that we left columns of height in the middle. If we use the notation , then we have reduced the problem to finding a subset of numbers which sum is precisely . Such numbers can be found using a greedy algorithm. The proof is left as an exercise to the reader.
Comments