Editorial for SAC '22 Code Challenge 1 P4 - That Problem


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.

Author: wleung_bvg

Subtask 1

We can check all (N4) tuples of (i,j,k,l) where i<j<k<l, check if Ai+Aj+Ak=Al, and increment our total.

Time Complexity: O(N4)

Subtask 2

Suppose we had an array cnt such that cnt[x][y] was equal to the number of indices l such that x<l and A[l]=y. We can then iterate through all (N3) tuples of (i,j,k), and add cnt[k][Ai+Aj+Ak] to our total. We can compute cnt[x][y] using suffix sums.

Time Complexity: O(N3+NmaxAi)

Subtask 3

Welcome to another wleung_bvg editorial. As usual, he still does not know how to do dynamic programming and is bad at grammar, so please continue to bear with him. Note that it is possible to solve this problem without dynamic programming.

Suppose we processed elements from left to right and maintained an array dp with dp[i][y][z] equal to the number of increasing subsequences ending at or before index i with a length of y and a sum of z. Then for each element, we can add dp[i1][3][Ai] to our total and update the dp array appropriately. We can update the dp array with the following recurrence:

dp[i][y][z]={dp[i1][y][z]+dp[i1][y1][zAi]y1,zAidp[i1][y][z]otherwise

The initial state of the dp array is all elements equal to 0 with dp[0][0][0]=1. Note that we only need to consider y3 and z100 for this problem. While using this recurrence directly uses O(NmaxAi) memory, it is possible to reduce it to O(maxAi).

Time Complexity: O(NmaxAi)


Comments

There are no comments at the moment.