When solving this problem, the first observation I made is that it is in fact hard, likely impossible, to check in a reasonable time if a given split into Bi or Ci satisfies the requirements or not, as it requires solving a pretty large instance of the knapsack problem. This may be the reason that the problem does not require to print a certificate, just a Yes/No answer.
This naturally leads to the following question: for which splits into Bi or Ci we can reasonably easily prove that achieving exactly half is impossible? To make such proof easier, it makes sense to split all even numbers exactly in half such that Bi=Ci: then we know for sure those numbers' contribution to the sum, and there are fewer possibilities to check. However, if all numbers are even and we do this for all numbers, then it would be possible to achieve exactly half of the total sum (in fact, it would be impossible to achieve anything else :)). But then we can do this even split for all numbers except one, and for one number (say, A1) we set B1=0 and C1=A1. Then we get exactly half from all other numbers, but if we choose B1 then the sum is slightly less than exactly half of the total, and if we choose C1 it is greater. Therefore we have solved the problem for the case where all Ai are even (the answer is always Yes).
What can we do about odd numbers? They cannot be split exactly in half, but we can try to build on the above construction: let us split all odd numbers almost in half, such that Bi+1=Ci, and split one number, the biggest one (assume we reorder the numbers and it is A1), as B1=0 and C1=A1. Now if the amount of odd numbers is less than A1, then we still cannot achieve exactly half, because if we choose B1, even taking Ci from all odd numbers will still leave us short of half of the total, and if we choose C1, we overshoot. There is a slight complication that happens when A1 is odd, as then we should not count it towards the amount of odd numbers we split almost in half; however, since the total amount of odd numbers is always even (because the sum is even), this does not affect our comparison and we can still compare if A1 is strictly greater than the total amount of odd numbers.
This criterion was my first submission, however, it got WA. As I did not have any immediate ideas for other situations where achieving exactly half is clearly impossible, I implemented a brute force solution and stress-tested it against this one. The smallest counterexample it produced was: 1, 3, 3, 3. In this case we set all Bi=0 and Ci=Ai and there is no way to achieve the required sum of 5 from some subset of 1, 3, 3, 3. The first idea after seeing this was that divisbility by 3 is somehow a factor; however, quite quickly I realized that we can slightly generalize the construction from the first submission above: we take all odd numbers, sort them, and split them into two parts of odd size. In the part containing the smaller numbers, we set Bi+1=Ci, and in the part containing the bigger numbers, we set Bi+D=Ci, where D is the smallest of those bigger numbers. Now if the size of the part with smaller numbers is less than D, then we always fall short of half of the total if we choose more Bi's than Ci's in the part with the bigger odd numbers, and we always overshoot otherwise.
This solution passed the stress-test against the brute force for small constraints, therefore I submitted it and it got accepted. I did not bother proving it formally since the stress-test was proof enough, but the intuition is somewhat clear: now we say No only if there are at least two odd numbers up to 1, at least four odd numbers up to 3, at least six odd numbers up to 5, and so on until we run out of odd numbers, and the total amount of odd numbers is at least the biggest number. I did not write down all details, but the following method likely works to achieve exactly half in this case: we first go through all even numbers, and then through all odd numbers in decreasing order. If the sum we accumulated so far is bigger than half of total of the numbers processed so far, we take the smaller one of Bi and Ci, otherwise the bigger one. We can now prove by induction that after processing all odd numbers except the x smallest ones, the current sum differs from half of all processed numbers by at most (x+1)/2, which means that in the end it is exactly equal.
Thanks for reading, and check back next week!
In problem H, n = 3e5 (not 1e5).
ReplyDeleteThanks! Fixed.
Delete