# Google Kick Start 2021 Round A Tutorial

## Problem A — K-Goodness String

First, we need to calculate the current score $$$K_c$$$. Since in one operation we can increase or decrease the score by $$$1$$$, the answer is $$$|K_c-K|$$$.

- Time complexity is $$$\mathcal{O}(|S|)$$$.
- Space complexity is $$$\mathcal{O}(1)$$$.

**Code (C++)**

## Problem B — L Shaped Plots

First, we calculate the length of longest continuous $$$1$$$ starting from each cell via simple dynamic programming, namely, $$$W[i][j]$$$, $$$E[i][j]$$$, $$$N[i][j]$$$ and $$$S[i][j]$$$.

Then we enumerate each cell and count the L-shaped plots centering at the cell. There are four types of L-shaped plots considering the orientation:

- WN
- WS
- EN
- ES

And for each type, we need to consider which side to be the longer side.

- Time complexity is $$$\mathcal{O}(RC)$$$.
- Space complexity is $$$\mathcal{O}(RC)$$$.

**Code (C++)**

## Problem C — Rabbit House

Obviously, the highest cell should not be increased. With this insight, we can solve this problem in a Dijkstra-like manner.

We use a max-heap to store the cells to be processed. When we process a cell with height $$$h$$$, we need to ensure that all its neighbors are at least as high as $$$h-1$$$. If the height of any of its neighbors is increased, then we should enqueue that cell with the new height.

In the end, we accumulate the difference between each cell's final height and original height to get the answer.

- Time complexity is $$$\mathcal{O}(RC\log(RC))$$$.
- Space complexity is $$$\mathcal{O}(RC\log(RC))$$$.

**Code (C++)**

## Problem D — Checksum

The hardest part of this problem is to recognize that it is actually a Minimum Spanning Tree (actually Maximum Spanning Tree, but they are equivalent) problem.

The first observation is that we can consider the known numbers to be unknown with a cost of $$$0$$$, and solve an equivalent problem in which all numbers are unknown.

The second observation is that we need to check exactly $$$(N-1)^2$$$ cells for a $$$N\times N$$$ matrix:

- If we check fewer than $$$(N-1)^2$$$, then at least $$$2N$$$ cells are unknown, while we can induce at most $$$2N-1$$$ cells.
- If we check more than $$$(N-1)^2$$$, then there is at least a row/column whose cells are all checked, which is unnecessary.

Now, instead of determining the $$$(N-1)^2$$$ cells to be checked, we try to determine the $$$2N-1$$$ cells which are induced. We want the sum of their costs to be maximized so that the sum of the checked cells can be minimized.

An insight is that, if we consider the $$$N$$$ rows and $$$N$$$ columns to be nodes, and the cells to be undirected edges (cell $$$(i,j)$$$ is considered to be an edge $$$r_i\leftrightarrow c_j$$$), then there will be exactly $$$2N$$$ nodes, while we need to choose $$$2N-1$$$ edges from all edges such that the sum of their weights is maximized. Very close to MST!

To confirm that it is exactly MST, we need to check whether loops are allowed. Since there are no edges that connect two rows or two columns, a loop must be in the form $$$r_0\leftrightarrow c_0\leftrightarrow r_1\leftrightarrow c_1\cdots\leftrightarrow r_0$$$. If a loop occurs, then all the rows and columns in this loop would have at least two unknown cells, which is unsolvable.

So we need to choose $$$2N-1$$$ edges in a graph with $$$2N$$$ nodes and loops are not allowed. Now we are confirmed that this is an MST.

The MST part is quite trivial, both Kruskal and Prim can work fluently.

- Time complexity is $$$\mathcal{O}(N^2\log N)$$$ for Kruskal, and $$$\mathcal{O}(N^2)$$$ for Prim (since it is a dense graph).
- Space complexity is $$$\mathcal{O}(N^2)$$$.

**Code (C++)**

In Problem D Suppose we have $$$5*5$$$ matrix with values :

matrixSuppose last three columns have known values and suppose in first two column we know only the value of x12 . Thus total $$$3*5+1 = 16 = (5-1)^2$$$ values are known . Now except $$$x11$$$ how other values are induced ?

Compulsory apologyApologies to 149 people who got pinged due to this comment.

There are loops in your example, so it is invalid.

I have taken arbitrary matrix , value xij can be 0 or 1 . Just we know some of those value (last three columns and x12) . How it's invalid ? I don't see any "loop" word in problem. If you mean two x41 , i corrected it , xij are just notation to denote a cell .

All known numbers can be seen as unknown with a cost of $$$0$$$, as I said in my editorial.

Ok, so after finding the edges in the maximum spanning tree of the graph, will we be adding the weights of edges not included in max. spanning tree to give the answer?

Yes, the answer is the total cost minus the cost of the maximum spanning tree.

having done this problem really helped :P

though I fkd up it in a hurry and used queue instead of PQ only to realize it at the very end of the contest.

Thanks for the solution, I have learned a lot from your blogs/post!!

hey lucifer1004 , one doubt

There might be some OS differences. During the contest, I used a lot of includes and I cleaned them up after the contest. But the sanitized version has only been tested on my Mac, so there might be some issues on other OS.

These are results from the contest judge, I tested these in the practice round, I guess some additional header is needed for structural binding.

Fixed. Should include

`tuple`

instead of`algorithm`

.The time limits were huge

Can somebody tell, the intuition of making graph with row and col(i was thinking to make with each cell in same row and col but edges were becoming too high) in question 4(checksum). How to think that intuitively. or tell the question which are related to this idea.

My take, from what I can make of it so far :

In problem C, can't we use multisource bfs?

We start with all the cell having height = max.

We check all its neighbours should have max — 1 height, if not increase their height and add diff in ans.

Once we reach a level end, we decrease max by 1. and keep going.

Can anyone suggest what is flow in this? https://pastebin.com/ECG6iBZX