Editorial for DMOPC '21 Contest 3 P2 - Weak Data
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For this problem, one first step is to consider the checker: how do we check that an answer is correct? How do we count the number of even subarrays?
The answer is semi-well-known. You might be able to find the answer on the internet, or you may have already solved a problem that tests this. At any rate, the basic idea is to keep track of the number of even prefixes and number of odd prefixes.
At the start, you have an even prefix of zero elements. Then, for each element, if the new prefix is even, any even prefix already seen can be used to make an even subarray, and if it is odd, any odd prefix can be used.
The reason for this is that if you have two prefixes, the space in the longer one but not the shorter one is a subarray. We include the zero length prefix to make sure we get all the prefixes. In order for the subarray formed to have even sum, the sums of the two prefixes have to have the same parity.
At this point, you may realize that it does not matter as to the position of the prefixes, but rather only the number of even prefixes and the number of odd prefixes. Specifically, if we have even prefixes, odd ones, and we define:
Then the number of even subarrays is . We have at least one even prefix.
First Subtask
For the first subtask, we can brute force all to get the number of even prefixes and odd prefixes. From there we can make a PSA and reverse it to get our original array. We remark that the original array can be constructed such that it has at most one odd number in all cases.
Time Complexity:
Second Subtask
For full marks, we can use a two-pointer approach. Iterate through all in increasing order and constantly decrease until we either find and , or output . Contestants should take care to handle and properly. The former should have , and the latter has a solution when the whole array is even and .
Time Complexity:
Comments