Editorial for APIO '07 P3 - Zoo


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 is a special case of Max-SAT, where each clause can only contain variables which are close together (in a circular order). Specifically:

  • Each animal is a boolean variable in Max-SAT. We have N variables X1,,XN in a circular order.
  • Each child is a clause in Max-SAT. If a child can see k consecutive enclosures, this means that each clause may only contain variables Xc,Xc+1,,Xc+k1 for some c (where XN+1=X1, XN+2=X2 and so on). For this problem, we are given k=5.

A model solution uses dynamic programming.

Linear case

Consider a simpler problem when the enclosures X1,,XN are placed in a linear order (i.e., XN does not wrap back around to X1). For each i, let Ci be the set of all children whose visible enclosures lie in the range X1,,Xi. We iterate through i=1,2,,N, and at each stage we solve problems of the following form:

Consider the set of children Ci, and consider all 2k ways in which the final k enclosures Xik+1,Xik+2,,Xi may be filled or empty. For each filled/empty combination for enclosures Xik+1,Xik+2,,Xi, what is the maximum number of children in Ci who can be made happy?

We solve the problems at stage i as follows. We assume we have already computed the maximum number of happy children in Ci1 for all filled/empty combinations for enclosures Xik,,Xi1. To calculate the new answers for stage i and a particular filled/empty combination for enclosures Xik+1,Xik+2,,Xi:

  1. We count how many "new" children in CiCi1 are made happy. We can calculate this directly from our choice of filled/empty values for Xik+1,Xik+2,,Xi, because every "new" child is looking at precisely these enclosures.
  2. Assuming the "old" enclosure Xik is filled, we can look up our solutions from stage i1 to find out how many children in Ci1 can be made happy with our selected filled/empty values for Xik+1,,Xi1. Likewise, we can look up how many children in Ci1 can be made happy when the old enclosure Xik is empty. The larger of these two numbers tells us how many "old" children in Ci1 we can keep happy overall.
  3. The number of happy "new" children from step 1 can be added to the number of happy "old" children from step 2, giving the solution to our current problem.

The total number of problems to solve is 2kN, and the overall running time for the algorithm is O(2k(N+C)). The space required is only O(2k), since each stage i only requires the solutions from the previous stage i1.

Circular case

In the circular case, we need to permanently remember the states of the first k1 enclosures X1,,Xk1 (in addition to the "most recent" k enclosures Xik+1,,Xi). This allows us to correctly deal with the final children who can see back around past XN to X1,,Xk1 again. The remainder of the algorithm remains more or less the same.

We therefore have 22k problems to solve at each stage, giving running time O(22k(N+C)) and storage space O(22k).


Comments

There are no comments at the moment.