1. Code Cards
Suppose length of is . If and differ at some index , then we can make and 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 and equal.
If it is possible, count number of occurrences where and differ and print that count.
2. Dev - the mad king
It was straight forward result of Chicken McNugget theorem, refer this excellent explanation on AoPS.
3. Bored Vivek
Task is to select at most laminates out of such that sum of their width is at most 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 .
Suppose is the maximum beauty if we choose laminates with width from first laminates.
For every new laminate we have a choice to either use it or not, if we use a laminate then becomes , becomes , otherwise and don’t change.
So,
We have to find maximum value of , such that and .
You can omit first state of to save memory as every layer depends only on the layer , it is shown here for better understanding.
4. Decode “DEV”
We can swap with adjacent or . So, if at any place comes after and both are adjacent we can swap them to get a better answer.
This means before first we can make a sorted prefix of . As an example if string was ,we can get . As no can be swapped with , first acts as a partition between s after and before first .
But all s can get through that, so if there are s in the complete string and s before the first . We can achieve .
Now after the first we’re left with the s that couldn’t get through and s. We can’t swap with so we’ll preserve the relative ordering between them after first .
5. Square Sum Numbers
Such type of tasks are popularly referred as Digit DP problems.
Let’s say we have a function which finds the answer for range
Using we have two options to find answer for range
- As can be as large as , calculating can take extra work, so we can calculate . Here returns if sum of digits of is a perfect square, otherwise .
Now I’ll discuss how to write .
Suppose is count of numbers such that we have fixed most significant digits whose sum is .
is if the number has already become smaller than i.e. there is some digit in first which is smaller than its counterpart at the same position in , otherwise tight is .
Now what digit can be placed at position is determined by , if number has already become smaller we can place anything from to as it is not going to make the number greater than , else we can only choose digits in range .
So if we choose digit , will become , and will remain if it was earlier and , otherwise we made the number smaller by choosing something else so will be .
NOTE: In the code below, the name is counter-intuitive it is in the code when number has become smaller than , otherwise which differs from the idea discussed above, but process remains the same.
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 which returns maximum number of coins a processed hero with has.
Now consider we are processing a hero with age and it has coins. So this hero can surely have coins by killing a processed hero which has maximum coins i.e. and its age is less than .
This 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 , so that it becomes a candidate for the queries where .