IZhO - Longest Beautiful Subsequence

Author: Daniel Zhu

Explanation

Solving this problem all at once is extremely difficult: let's go through the subtasks first.

Subtasks 1-2

Due to the small bounds on NN, an O(N2)\mathcal{O}(N^2) solution suffices.

Define dp[i]\texttt{dp}[i] as the length of the longest LBS (longest beautiful subsequence) that ends at ii, and transk(y)\texttt{trans}_k(y) as all xx such that x&yx \hspace{3pt} \& \hspace{3pt} y has kk bits. We transition by iterating over all j<ij < i where ajtranski(ai)a_j \in \texttt{trans}_{k_i}(a_i). To reconstruct an optimal solution we define another array prv\texttt{prv}, where prv[i]\texttt{prv}[i] is the most optimal index to include directly before index ii.

Note

We use bc[x][y]\texttt{bc}[x][y] below to denote bit_count(x&y)\texttt{bit\_count}(x \hspace{3pt} \& \hspace{3pt} y).

Consider the first sample case: n=4n = 4, a={1,2,3,4}a = \{1, 2, 3, 4\}, and k={10,0,1,0}k = \{10, 0, 1, 0\}.

  • Base case: dp[i]=1\texttt{dp}[i] = 1 for all ii (any single number forms an LBS) and prv[i]=i\texttt{prv}[i] = i.
  • dp[1]\texttt{dp}[1]: we try extending the longest LBS that ends at a0=1a_0 = 1. Because bc[a0][a1]==k1\texttt{bc}[a_0][a_1] == k_1, our new LBS is valid, and we update dp[1]=dp[0]+1=2\texttt{dp}[1] = \texttt{dp}[0] + 1 = 2 and prv[1]=0\texttt{prv}[1] = 0.
  • dp[2]\texttt{dp}[2]: we try to extend the LBS from both a0a_0 and a1a_1.
    • a0a_0: bc[a0][a2]==k2dp[2]=dp[0]+1=2\texttt{bc}[a_0][a_2] == k_2 \rightarrow \texttt{dp}[2] = \texttt{dp}[0] + 1 = 2 and prv[2]=0\texttt{prv}[2] = 0
    • a1a_1: bc[a1][a2]==k2dp[2]=dp[1]+1=3\texttt{bc}[a_1][a_2] == k_2 \rightarrow \texttt{dp}[2] = \texttt{dp}[1] + 1 = 3 and prv[2]=1\texttt{prv}[2] = 1
    • We take the larger of the two DP values, so dp[2]=3\texttt{dp}[2] = 3 and prv[2]=1\texttt{prv}[2] = 1.
  • dp[3]\texttt{dp}[3]: we try to extend from a0a_0, a1a_1, and a2a_2.
    • a0a_0: bc[a0][a3]k3\texttt{bc}[a_0][a_3] \neq k_3 \rightarrow no transition
    • a1a_1: bc[a1][a3]==k3dp[3]=dp[1]+1=3\texttt{bc}[a_1][a_3] == k_3 \rightarrow \texttt{dp}[3] = \texttt{dp}[1] + 1 = 3 and prv[3]=1\texttt{prv}[3] = 1
    • a2a_2: bc[a2][a3]==k3dp[3]=dp[2]+1=4\texttt{bc}[a_2][a_3] == k_3 \rightarrow \texttt{dp}[3] = \texttt{dp}[2] + 1 = 4 and prv[3]=2\texttt{prv}[3] = 2
    • Taking the larger of the two options, dp[3]=4\texttt{dp}[3] = 4 and prv[3]=2\texttt{prv}[3] = 2.
  • Final answer: len=max(dp[i]i[0,n))=dp[3]=4\texttt{len} = \max(\texttt{dp}[i]_{i \in [0, n)}) = \texttt{dp}[3] = \boxed{4}
  • Reconstructing a solution:
    • Using our prv\texttt{prv} array and the ending index of the LBS, we can repeatedly find the optimal index to include before the current one.
    • choose ii such that dp[i]=len=4i=3\texttt{dp}[i] = \texttt{len} = 4 \Rightarrow i = 3
    • intialize ansans as {i}={3}\{i\} = \{3\}.
    • We repeatedly append prv[ans0]\texttt{prv}{[ans_0]} to the front of ansans.
      • ans={prv[3],3}={2,3}ans = \{\texttt{prv}[3], 3\} = \{2, 3\}
      • ans={prv[2],2,3}={1,2,3}ans = \{\texttt{prv}[2], 2, 3\} = \{1, 2, 3\}
      • ans={prv[1],1,2,3}={0,1,2,3}ans = \{\texttt{prv}[1], 1, 2, 3\} = \{0, 1, 2, 3\}
      • prv[0]=0\texttt{prv}[0] = 0, so we terminate.
    • Therefore, our final (0-indexed) answer will be ans={0,1,2,3}.ans = \boxed{\{0, 1, 2, 3\}}.

Implementation

Time Complexity: O(N2)\mathcal{O}(N^2)

Code

Subtask 3

While the bounds on NN are now much larger (N105N \leq 10^5), the maximum value of aia_i is now only M=28M = 2^8. We can use this to optimize our solution: rather than looping over every possible previous index, we can loop over all possible previous values instead.

More specifically, we define dp1[i][x]\texttt{dp1}[i][x] to be the length of the longest LBS that ends with value xx (rather than index) and is a subset of the first ii numbers. We have two transitions for each index ii:

  • Include aia_i: to calculate dp1[i][ai]\texttt{dp1}[i][a_i], we transition from dp1[i1][x]\texttt{dp1}[i - 1][x] for all xtranski(ai)x \in \texttt{trans}_{k_i}(a_i).
  • Don't include aia_i: dp1[i][x]=dp1[i1][x]\texttt{dp1}[i][x] = \texttt{dp1}[i - 1][x] for all xMx \leq M.

Implementation Note

In order to reconstruct a solution, we need to store the ending index of the optimal LBS in dp1[i][x]\texttt{dp1}[i][x] in addition to its length. In our implementation, our State object holds an end property in addition to a len property. In our example below, we represent each state as {len, end}.

This improves our time complexity to O(NM)\mathcal{O}(NM). Notice that, because dp1[i][x]\texttt{dp1}[i][x] only depends on dp1[i1][...]\texttt{dp1}[i - 1][...], we can drop the first dimension.

Consider the first sample case again: n=4n = 4, a={1,2,3,4}a = \{1, 2, 3, 4\}, and k={10,0,1,0}k = \{10, 0, 1, 0\}. The maximum value is M=4M = 4.

  • Base case: dp1[a0=1]={1,0}\texttt{dp1}[a_0 = 1] = \{1, 0\}.
    • all other DP values are initially 0
    • prv[i]=i\texttt{prv}[i] = i
  • dp1[a1=2]={2,1}\texttt{dp1}[a_1 = 2] = \{2, 1\}.
    • we loop over all states dp1[x]\texttt{dp1}[x] where xMx \leq M and xx is in transki(ai)\texttt{trans}_{k_i}(a_i). The values of xx that satisfy these conditions are 00, 11, and 44.
    • Out of these xx's, dp1[x=1]\texttt{dp1}[x = 1] has the greatest value of len, so:
      • dp1[a1].len=dp1[x].len+1=2\texttt{dp1}[a_1]. \texttt{len} = \texttt{dp1}[x].\texttt{len} + 1 = 2.
      • dp1[a1].end=i=1\texttt{dp1}[a_1].\texttt{end} = i = 1.
      • prv[i]=dp1[x].end=0\texttt{prv}[i] = \texttt{dp1}[x].\texttt{end} = 0.
  • dp1[a2=3]={3,2}\texttt{dp1}[a_2 = 3] = \{3, 2\}.
    • here, x{1,2}x \in \{1, 2\} and dp1[x=2]\texttt{dp1}[x = 2] has the greater value, so:
      • dp1[a2].len=dp1[x].len+1=3\texttt{dp1}[a_2]. \texttt{len} = \texttt{dp1}[x].\texttt{len} + 1 = 3.
      • dp1[a2].end=i=2\texttt{dp1}[a_2].\texttt{end} = i = 2.
      • prv[i]=dp1[x].end=1\texttt{prv}[i] = \texttt{dp1}[x].\texttt{end} = 1.
  • dp1[a3=4]={4,3}\texttt{dp1}[a_3 = 4] = \{4, 3\}.
    • here, x{0,1,2,3}x \in \{0, 1, 2, 3\} and dp1[x=3]\texttt{dp1}[x = 3] has the greatest value so:
      • dp1[a3].len=dp1[x].len+1=4\texttt{dp1}[a_3]. \texttt{len} = \texttt{dp1}[x].\texttt{len} + 1 = 4.
      • dp1[a3].end=i=3\texttt{dp1}[a_3].\texttt{end} = i = 3.
      • prv[i]=dp1[x].end=2\texttt{prv}[i] = \texttt{dp1}[x].\texttt{end} = 2.
  • Final answer: the max DP len value, 4\boxed{4}.

Implementation

Time Complexity: O(NM)\mathcal{O}(NM)

Code

Subtask 3 (Alternate Solution)

In our previous solution for this Subtask 3, the loop from 0...M0...M seems repetitive and motivates somehow restricting the search space of DP states. How can we quickly process all xx in transki(ai)\texttt{trans}_{k_i}(a_i)?

The most extreme way to optimize transitions is to embed the restrictions of xx directly in the DP state.

Concretely, define dp2[i][k]\texttt{dp2}[i][k] as the maximum value of dp1[x]\texttt{dp1}[x] over all xx in transk(i)\texttt{trans}_k(i).

To better understand this redefinition of our DP state, let's look at our sample again. Notice, in our computation of dp1[a3=4]\texttt{dp1}[a_3 = 4], we process all x{0,1,2,3}x \in \{0, 1, 2, 3\} one by one. However, in our newly defined state, we can get the most optimal solution over all values xx simply by querying dp2[a3][k3]\texttt{dp2}[a_3][k_3].

In general, dp2[ai][ki]\texttt{dp2}[a_i][k_i] is now an O(1)\mathcal{O}(1) transition to find the longest LBS that ends at index ii.

However, each state dp2[i][k]\texttt{dp2}[i][k] now encapsulates transk(i)M|\texttt{trans}_k(i)| \leq M of our original dp1\texttt{dp1} states. Similarly, an LBS with fixed final value aia_i affects MM dp2\texttt{dp2} states compared to one dp1\texttt{dp1} state: namely, all states dp2[x][k]\texttt{dp2}[x][k] where x[0,M)x \in [0, M) and k=bc[ai][x]k = \texttt{bc}[a_i][x].

Implementation (dp2)

Code

Subtask 4 (Full Solution)

In summary, if xx is the last number in an LBS:

  • dp1\texttt{dp1} (only one possible value of xx) O(M)\rightarrow \mathcal{O}(M) transition, O(1)\mathcal{O}(1) affected states
  • dp2\texttt{dp2} (up to MM possible values of xx) O(1)\rightarrow \mathcal{O}(1) transition, O(M)\mathcal{O}(M) affected states

Notice that, no matter how we define the DP state, these two complexities will always multiply to O(M)\mathcal{O}(M). Therefore, we want to find a restriction such that both complexities are O(M)\mathcal{O}(\sqrt{M}). This motivates using a blend of dp1\texttt{dp1} and dp2\texttt{dp2}. Since dp12\texttt{dp1}2 already restricts bits, it seems natural to consider applying dp1\texttt{dp1} on some bits of xx and dp2\texttt{dp2} on the rest.

Concretely, let lx=l(x,b)l_x = \texttt{l}(x, b) be the number represented by the bb leftmost bits of xx and rx=r(x,b)r_x = \texttt{r}(x, b) be the number represented by the bit_count(x)b\texttt{bit\_count}(x) - b rightmost bits. In our new DP state dp[i][j][k]\texttt{dp}[i][j][k], we will:

  • fix lx=il_x = i (this is the dp1\texttt{dp1} part of our new state)
  • restrict rxr_x such that rxtransk(j)bc[rx][j]=kr_x \in \texttt{trans}_k(j) \Rightarrow \texttt{bc}[r_x][j] = k (this is the dp2\texttt{dp2} part of our new state)

What are the time complexities of transition and affected states now?

Transition

Let's say we're currently processing aia_i. If we have xx such that xtranski(ai)x \in \texttt{trans}_{k_i}(a_i), then bc[x][ai]=ki\texttt{bc}[x][a_i] = k_i. By considering the left and right sides of xx and aia_i separately, we can rewrite bc[x][ai]\texttt{bc}[x][a_i] as bc[lx][lai]+bc[rx][rai]=ki\texttt{bc}[l_x][l_{a_i}] + \texttt{bc}[r_x][r_{a_i}] = k_i.


Example: xx = 01010, aia_i = 01101, bb = 3

01010 & 01101 = 01000 -> 1

010   & 011   = 010   -> 1

   10 &    01 =    00 -> 0

1+0=11 + 0 = \boxed1.


Since our DP state dp[l][r][k]\texttt{dp}[l][r][k] fixes lx=ll_x = l, we should rewrite this equation to solve for rxr_x: bc[rx][rai]=kibc[lx][lai]\texttt{bc}[r_x][r_{a_i}] = k_i - \texttt{bc}[l_x][l_{a_i}]. Note this restriction matches our dp\texttt{dp} restriction of bc[rx][j]=k\texttt{bc}[r_x][j] = k, so we have r=rair = r_{a_i} and k=kibc[lx][lai]=kibc[x][lai]k = k_i - \texttt{bc}[l_x][l_{a_i}] = k_i - \texttt{bc}[x][l_{a_i}]. As for l=lx=l(x,b)l = l_x = \texttt{l}(x, b), we simply enumerate all 2b2^b possibilities; therefore, i[0,2b)i \in [0, 2^b).

In summary, the transitions to aia_i are all states dp[l][rai][kibc[l][lai]]\texttt{dp}[l][r_{a_i}][k_i - \texttt{bc}[l][l_{a_i}]] where l[0,2b)l \in [0, 2^b).

Because the only variable in the state is ll, the complexity of transition is the number of options for ll which is 2b\boxed{2^b}.

Affected States

We now have the length of the longest LBS that ends at aia_i. What dp[l][r][k]\texttt{dp}[l][r][k] states does this affect?

Starting with the obvious, l=lail = l_{a_i}. We also have the restriction that raitransk(r)k=bc[rai][r]r_{a_i} \in \texttt{trans}_k(r) \Rightarrow k = \texttt{bc}[r_{a_i}][r]. Because rair_{a_i} is a constant, kk depends solely on rr.

In summary, the states affected by aia_i are all states dp[lai][r][bc[rai][r]]\texttt{dp}[l_{a_i}][r][\texttt{bc}[r_{a_i}][r]].

rr has to have the same amount of bits as rai=r(ai,b)r_{a_i} = \texttt{r}(a_i, b) which has log2Mb\log_2{M} - b bits. Therefore, rr has 2log2Mb=M2b2^{\log_2M - b} = \boxed{\frac{M}{2^b}} possibilities.

To sum it all up, the complexity of transition is O(2b)\mathcal{O}(2^b) and the complexity of affected states is O(M2b)\mathcal{O}(\frac{M}{2^b}). To minimize the sum of these complexities, we should equate them. Because M=220M = 2 ^{20}, we find the optimal value of bb to be 10\boxed{10}.

Implementation

In the implementation below:

  • l(x)\texttt{l}(x) denotes the 10 leftmost bits of xx
  • r(x)\texttt{r}(x) denotes the 10 righmost bits of xx

Time Complexity: O(NM)\mathcal{O}(N\sqrt{M})

Code

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!