Editorial for ICPC PACNW 2016 J - Shopping
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.
First, this problem is equivalent to a cumulative mod over a range (that is, we take ). The second thing to notice is that if , then (to prove this, consider two cases: and ).
This shows that any one shopper can buy at most distinct items.
We can solve this by reducing it to the subproblem: given a range and a number , find the smallest index where , or report none exists. This can be solved with a range tree.
Alternatively, we can also solve this using a heap and processing items left to right.
Comments