Editorial for WC '16 Finals S3 - Privacy


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.

This problem can be solved with dynamic programming. Let DP[i][j] be the minimum number of treats required to satisfy the first i cows (cows 1 \dots i) while dividing them into j troughs. The base case is DP[0][0] = 0, and the final answer will be DP[N][K+1], as we'll be installing K dividers to divide the N cows into K+1 troughs.

From a given state (i, j), the only choice is regarding how many cows should be included in the next trough. The first cow in the trough will be i, and the last will be k (for some k between i and N, inclusive). We should consider each possible value of k. For a given k, there will be c = k-i+1 cows in the next trough. The number of treats t required to satisfy each of those c cows can easily be computed by iterating over them (and comparing their C values to the cow count c). We end up with the transition DP[k][j+1] = \min(DP[k][j+1], DP[i][j]+t).

A direct implementation of the approach described above would have a time complexity of \mathcal O(N^3 K) – we'll consider each of the \mathcal O(NK) states in turn (for i from 0 to N-1 and from j from 0 to K), consider each of the \mathcal O(N) possible transitions from it, and then compute each transition's t cost in another \mathcal O(N) time. This is too slow to earn full marks. However, the time complexity can be improved to \mathcal O(N^2(N+K)) by essentially just swapping the nesting order of the j and k loops. Note that each transition's t cost depends only on i and k, so there's no need to recompute it \mathcal O(K) times for all possible values of j.


Comments

There are no comments at the moment.