SEABOX - Editorial

Difficulty:

Medium-hard

Pre-Requisites:

DP(Knapsack)

Problem Statement

You are given a fragment of code and binary 3-dimensional array $A[0..N-1, 0..N-1, 0..N-1]$, where $N$ can be only power of $2$, $1 <= N <= 32$. Consider all 3-dimensional arrays, which can be created with changing no more than $K$ elements of $A$ and apply this code for them. After that, we are interested in finding the minimal and maximal value of resulting function over all such arrays. Below is the fragment of code given in the statement:

Explanation

Subtask 1

For this subtask, we can observe that array $A$ has $N^3$ binary cells. Let's compress it in one-dimensional array $B$ of size $N^3$. $B_{i*N^2+j*N+k}$ = $A_{i,j,k}$, after that let's iterate over all bitmasks. Assume that $i$-th element will be $i$-th bit in a mask, then we need to iterate in the range from $0$ to $2^{N^3}-1$, check the number of different bits if this number not more than $K$ then launch the code above and relax the minimal and maximal values.

Subtasks 2-5

Let's try to understand what is the function $F$ in the statement. Let's make several observations:

We always deal with cube(3-dimensional array) $A[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1]$ in the function $F$

If all elements in cube $A[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1]$ are equal, then result of the function is $1$.

Otherwise, we divide our box into 8 equal sub-boxes, and sum the resulting values for them.

Variable "size" is a power of 2

What we can do with it? Let's denote couple of functions:

$minValue[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1][i]$ - the minimal value of function $F$, which we can get, if we'll change not more than $i$ values inside cube $A[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1]$, $0 <= i <= K$

$maxValue[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1][i]$ - the maximal value of function $F$, which we can get, if we'll change not more than $i$ values inside cube $A[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1]$, $0 <= i <= K$

$zeroes[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1]$ - a number of zeroes inside cube $A[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1]$

$ones[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1]$ - a number of ones inside cube $A[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1]$

In which cases $maxValue[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1][i]$ will be 1? Only in that case if $i = 0$ and all values inside cube are zeroes or ones. Because if $i > 0$, and all the values equal, we can change states exactly $1$ of them, after that value of $F$ will be recalculated using smaller sub-boxes.

In which cases $minValue[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1][i]$ will be 1? Only in those cases
when we can receive equal values inside the cube and change not more than $i$ values inside of it. In other words, if

$max(zeroes[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1],$

$ones[dx..dx+size-1,dy..dy+size-1,dz..dz+size-1])+i>=size^3$

Otherwise, how to recalculate value of $minValue$ and $maxValue$ using smaller sub-cubes and values of $minValue$ and $maxValue$ for them. It will be very similar to knapsack problem. $minValue[cube][i]$ can be calculated if we don't change more than $i$ cells inside 8 smaller subcubes. Analogically with $maxValue$.