Editorial for COCI '14 Contest 7 #5 Prosjek
Submitting an official solution before solving the problem yourself is a bannable offence.
It is a difficult task to directly answer what the maximal possible average is, so let's instead try to answer the question of whether the maximal possible average is larger than or equal to some value . If we succeed in this, then we can solve the task using binary search over .
This task also seems difficult, but let's notice this: if we subtract from every number in the array, then the average of all subsequences is reduced by exactly . Now we can subtract from all the numbers in the array and see if a subsequence exists with an average larger than or equal to . This task is simpler because it is equivalent to the question of whether a subsequence exists with the sum larger than or equal to zero.
If we calculate the prefix sum of the input array, then the sum of a subsequence is equal to . For every possible , we can ask ourselves whether such exists that , or . Also, we mustn't forget the condition for the minimal subsequence length, or .
Iterating over boundary value from left to right, we can store the minimal value seen so far in a special variable and check if it meets the requirement for the current . If it does, we have found a subsequence with an average larger than or equal to .
Each time we increment variable by , exactly one new value comes into consideration for and it is . By doing so, we check if is smaller than the current minimal value and update it if it is.
Comments