[Tutorial] Codecadet XXII

2021/09/25

1. Code Cards

Suppose length of S1S1 is nn. If S1S1 and S2S2 differ at some index ii, then we can make S1iS1_i and S2iS2_i equal only when one of them is $ and other is one of the letters in the set { c, o, d, e}.

Otherwise, it is impossible to make S1S1 and S2S2 equal.

If it is possible, count number of occurrences where S1S1 and S2S2 differ and print that count.

Solution


2. Dev - the mad king

It was straight forward result of Chicken McNugget theorem, refer this excellent explanation on AoPS.

Solution


3. Bored Vivek

Task is to select at most LL laminates out of NN such that sum of their width is at most WW and sum of their beauty is maximum.

It is classic knapsack problem with one simple twist that number of laminates can’t be more than LL.

Suppose dp[i][wt][ct]dp[i][wt][ct] is the maximum beauty if we choose ctct laminates with width wtwt from first ii laminates.

For every new laminate we have a choice to either use it or not, if we use a laminate jj then wtwt becomes wt+w[j]wt+w[j], ctct becomes ct+1ct+1, otherwise wtwt and ctct don’t change.

So, dp[i][wt][ct]=maxj<i ( dp[i1][wtw[j]][ct1]+ beauty[j], dp[i1][wt][ct])dp[i][wt][ct]=\max\limits_{j<i} \space (\space dp[i-1][wt-w[j]][ct-1]+\space beauty[j],\space dp[i-1][wt][ct])

We have to find maximum value of dp[N][wt][ct]dp[N][wt][ct], such that wtWwt \leq W and ctLct \leq L.

You can omit first state of dpdp to save memory as every layer ii depends only on the layer i1i-1, it is shown here for better understanding.

Solution


4. Decode “DEV”

We can swap EE with adjacent DD or VV. So, if at any place DD comes after EE and both are adjacent we can swap them to get a better answer.

This means before first VV we can make a sorted prefix of D,E{ D,E }. As an example if string was DEDEEV.DEDEEV….,we can get DDEEEVDDEEEV…. As no DD can be swapped with VV , first VV acts as a partition between DDs after and before first VV.

But all EEs can get through that, so if there are XX EEs in the complete string and YY DDs before the first VV. We can achieve DXEYV.D_XE_YV…..

Now after the first VV we’re left with the DDs that couldn’t get through and VVs. We can’t swap DD with VV so we’ll preserve the relative ordering between them after first VV.

Solution


5. Square Sum Numbers

Such type of tasks are popularly referred as Digit DP problems.

Let’s say we have a function calc(X)calc(X) which finds the answer for range [1,X][1, X]

Using calccalc we have two options to find answer for range [L,R][L,R]

Now I’ll discuss how to write calc(x)calc(x).

Suppose dp[i][sum][tight]dp[i][sum][tight] is count of numbers such that we have fixed ii most significant digits whose sum is sumsum.

tighttight is 00 if the number has already become smaller than xx i.e. there is some digit in first ii which is smaller than its counterpart at the same position in xx, otherwise tight is 11.

Now what digit can be placed at i+1thi+1_{th} position is determined by tighttight, if number has already become smaller we can place anything from 00 to 99 as it is not going to make the number greater than xx, else we can only choose digits in range [0,xi+1][0,x_{i+1}].

So if we choose digit dd, sumsum will become sum+dsum+d, and tighttight will remain 11 if it was 11 earlier and d=xi+1d = x_{i+1}, otherwise we made the number smaller by choosing something else so tighttight will be 00.

NOTE: In the code below, the name tighttight is counter-intuitive it is 11 in the code when number has become smaller than xx, otherwise 00 which differs from the idea discussed above, but process remains the same.

Solution


6. The combat of ages

As no hero can kill someone stronger, we can process all heroes in increasing order of their strength. If two heroes have equal strength then we process the hero with smaller age first because doing so gives more coins to older heroes when they will kill this one.

Let’s say we have a magic function best(X)best(X) which returns maximum number of coins a processed hero with ageXage \leq X has.

Now consider we are processing a hero with age AA and it has CC coins. So this hero can surely have C+best(A1)C+best(A-1) coins by killing a processed hero which has maximum coins i.e. best(A1)best(A-1) and its age is less than AA.

This best(X)best(X) function can be implemented by using any range update range query data structure, as we are querying only the prefix we can use Fenwick tree here for easier implementation.

After the score of current player is determined we can update the tree with value C+best(A1)C+best(A-1), so that it becomes a candidate for the queries best(X)best(X) where X>AX>A.

Solution