Notes for competitive programming
•
Codeforces
•
1042c-prod Array Product
Let’s deal with a simpler problem than what is asked for.
Given a list , determine the subset of that will give the maximum product.
Obviously, must contain all positive elements of . Since the product of two negative numbers is positive, then must contain an even number of negative numbers. If there are an odd number of negative numbers, we exclude the one least in magnitude, i.e. the largest negative number.
Thus, to get the maximum product of , we have to delete everything in that is zero; if has an odd number of negative numbers, we must delete that too.
Since we only have a single delete operation available to us, we have to merge everything in , then delete the merged element. We can then proceed to merge everything in .
Depending on the method you choose to implement this, pay attention to some edge cases:
- all positive values or all negative values, with being even - in both cases, nothing should be deleted;
- all negative values with being odd;
- all zero values;
- a mix of negatives and positives but with no zero-value.
- Editorial created
- Last updated
- Solution summary
Merge all undesirable elements, delete the merge, then merge the rest.
- Running time
Problem link
On CodeforcesTags
constructive-algorithm