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 N, an O(N2) solution suffices.
Define dp[i] as the length of the longest LBS (longest beautiful subsequence) that ends at i, and transk(y) as all x such that x&y has k bits. We transition by iterating over all j<i where aj∈transki(ai). To reconstruct an optimal solution we define another array prv, where prv[i] is the most optimal index to include directly before index i.
Note
We use bc[x][y] below to denote bit_count(x&y).
Consider the first sample case: n=4, a={1,2,3,4}, and k={10,0,1,0}.
Base case:dp[i]=1 for all i (any single number forms an LBS) and prv[i]=i.
dp[1]: we try extending the longest LBS that ends at a0=1. Because bc[a0][a1]==k1, our new LBS is valid, and we update dp[1]=dp[0]+1=2 and prv[1]=0.
dp[2]: we try to extend the LBS from both a0 and a1.
a0: bc[a0][a2]==k2→dp[2]=dp[0]+1=2 and prv[2]=0
a1: bc[a1][a2]==k2→dp[2]=dp[1]+1=3 and prv[2]=1
We take the larger of the two DP values, so dp[2]=3 and prv[2]=1.
dp[3]: we try to extend from a0, a1, and a2.
a0: bc[a0][a3]=k3→ no transition
a1: bc[a1][a3]==k3→dp[3]=dp[1]+1=3 and prv[3]=1
a2: bc[a2][a3]==k3→dp[3]=dp[2]+1=4 and prv[3]=2
Taking the larger of the two options, dp[3]=4 and prv[3]=2.
Final answer:len=max(dp[i]i∈[0,n))=dp[3]=4
Reconstructing a solution:
Using our prv array and the ending index of the LBS, we can repeatedly find the optimal index to include before the current one.
choose i such that dp[i]=len=4⇒i=3
intialize ans as {i}={3}.
We repeatedly append prv[ans0] to the front of ans.
ans={prv[3],3}={2,3}
ans={prv[2],2,3}={1,2,3}
ans={prv[1],1,2,3}={0,1,2,3}
prv[0]=0, so we terminate.
Therefore, our final (0-indexed) answer will be ans={0,1,2,3}.
Implementation
Time Complexity: O(N2)
Code
Subtask 3
While the bounds on N are now much larger (N≤105), the maximum value of ai is now only M=28. 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] to be the length of the longest LBS that ends with valuex (rather than index) and is a subset of the first i numbers. We have two transitions for each index i:
Include ai: to calculate dp1[i][ai], we transition from dp1[i−1][x] for all x∈transki(ai).
Don't include ai: dp1[i][x]=dp1[i−1][x] for all x≤M.
Implementation Note
In order to reconstruct a solution, we need to store the ending index of the optimal LBS in 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). Notice that, because dp1[i][x] only depends on dp1[i−1][...], we can drop the first dimension.
Consider the first sample case again: n=4, a={1,2,3,4}, and k={10,0,1,0}. The maximum value is M=4.
Base case:dp1[a0=1]={1,0}.
all other DP values are initially 0
prv[i]=i
dp1[a1=2]={2,1}.
we loop over all states dp1[x] where x≤M and x is in transki(ai). The values of x that satisfy these conditions are 0, 1, and 4.
Out of these x's, dp1[x=1] has the greatest value of len, so:
dp1[a1].len=dp1[x].len+1=2.
dp1[a1].end=i=1.
prv[i]=dp1[x].end=0.
dp1[a2=3]={3,2}.
here, x∈{1,2} and dp1[x=2] has the greater value, so:
dp1[a2].len=dp1[x].len+1=3.
dp1[a2].end=i=2.
prv[i]=dp1[x].end=1.
dp1[a3=4]={4,3}.
here, x∈{0,1,2,3} and dp1[x=3] has the greatest value so:
dp1[a3].len=dp1[x].len+1=4.
dp1[a3].end=i=3.
prv[i]=dp1[x].end=2.
Final answer: the max DP len value, 4.
Implementation
Time Complexity: O(NM)
Code
Subtask 3 (Alternate Solution)
In our previous solution for this Subtask 3, the loop from 0...M seems repetitive and motivates somehow restricting the search space of DP states. How can we quickly process all x in transki(ai)?
The most extreme way to optimize transitions is to embed the restrictions of x directly in the DP state.
Concretely, define dp2[i][k] as the maximum value of dp1[x] over all x in transk(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], we process all x∈{0,1,2,3} one by one. However, in our newly defined state, we can get the most optimal solution over all values x simply by querying dp2[a3][k3].
In general, dp2[ai][ki] is now an O(1) transition to find the longest LBS that ends at index i.
However, each state dp2[i][k] now encapsulates ∣transk(i)∣≤M of our original dp1 states. Similarly, an LBS with fixed final value aiaffects Mdp2 states compared to one dp1 state: namely, all states dp2[x][k] where x∈[0,M) and k=bc[ai][x].
Implementation (dp2)
Code
Subtask 4 (Full Solution)
In summary, if x is the last number in an LBS:
dp1 (only one possible value of x) →O(M) transition, O(1) affected states
dp2 (up to M possible values of x) →O(1) transition, O(M) affected states
Notice that, no matter how we define the DP state, these two complexities will always multiply to O(M). Therefore, we want to find a restriction such that both complexities are O(M). This motivates using a blend of dp1 and dp2. Since dp12 already restricts bits, it seems natural to consider applying dp1 on some bits of x and dp2 on the rest.
Concretely, let lx=l(x,b) be the number represented by the b leftmost bits of x and rx=r(x,b) be the number represented by the bit_count(x)−b rightmost bits. In our new DP state dp[i][j][k], we will:
fix lx=i (this is the dp1 part of our new state)
restrict rx such that rx∈transk(j)⇒bc[rx][j]=k (this is the 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 ai. If we have x such that x∈transki(ai), then bc[x][ai]=ki. By considering the left and right sides of x and ai separately, we can rewrite bc[x][ai] as bc[lx][lai]+bc[rx][rai]=ki.
Since our DP state dp[l][r][k] fixes lx=l, we should rewrite this equation to solve for rx: bc[rx][rai]=ki−bc[lx][lai]. Note this restriction matches our dp restriction of bc[rx][j]=k, so we have r=rai and k=ki−bc[lx][lai]=ki−bc[x][lai]. As for l=lx=l(x,b), we simply enumerate all 2b possibilities; therefore, i∈[0,2b).
In summary, the transitions to ai are all states dp[l][rai][ki−bc[l][lai]] where l∈[0,2b).
Because the only variable in the state is l, the complexity of transition is the number of options for l which is 2b.
Affected States
We now have the length of the longest LBS that ends at ai. What dp[l][r][k] states does this affect?
Starting with the obvious, l=lai. We also have the restriction that rai∈transk(r)⇒k=bc[rai][r]. Because rai is a constant, k depends solely on r.
In summary, the states affected by ai are all states dp[lai][r][bc[rai][r]].
r has to have the same amount of bits as rai=r(ai,b) which has log2M−b bits. Therefore, r has 2log2M−b=2bM possibilities.
To sum it all up, the complexity of transition is O(2b) and the complexity of affected states is O(2bM). To minimize the sum of these complexities, we should equate them. Because M=220, we find the optimal value of b to be 10.
Implementation
In the implementation below:
l(x) denotes the 10 leftmost bits of x
r(x) denotes the 10 righmost bits of x
Time Complexity:O(NM)
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!