Editorial for DMOPC '19 Contest 2 P4 - A Greedy Problem


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.

Author: george_chen

The intended solution for subtask 1 is to run 0-1 knapsack for each pair of problems. We simply run knapsack without a_i and b_i and then take the number of ways to get a sum of times not greater than q_i-t_{a_i}-t_{b_i}.

Time complexity: \mathcal{O}(NTQ)

There is no intended solution for the second subtask.

Time complexity: \mathcal{O}(NTQ)

For the third subtask, we observe that 0-1 knapsack can be thought of as a generating function:

\displaystyle g(x) = (1+x^{t_1}) \times (1+x^{t_2}) \times \dots \times (1+x^{t_N})

We see that each item is just a factor of the polynomial which means that items can be removed from the knapsack. To remove an item, we loop in increasing values of time and subtract values instead of adding them.

Therefore, we can add all of the items to the knapsack before all the queries. When we answer a query, we just remove a_i and b_i from the knapsack and check the number of ways to get a sum of times not greater than q_i-t_{a_i}-t_{b_i}.

Time complexity: \mathcal{O}((N+Q)T)


Comments

There are no comments at the moment.