### lucifer1004's blog

By lucifer1004, 2 months ago,

# 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++)

• +99

 » 2 months ago, # | ← Rev. 4 →   0 In Problem D Suppose we have $5*5$ matrix with values : matrixx11 x12 x13 x14 x15 x21 x22 x23 x24 x25 x31 x32 x33 x34 x35 x41 x42 x43 x44 x45 x51 x52 x53 x54 x55 Suppose 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.
•  » » 2 months ago, # ^ |   0 There are loops in your example, so it is invalid.
•  » » » 2 months ago, # ^ | ← Rev. 2 →   0 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 .
•  » » » » 2 months ago, # ^ |   0 All known numbers can be seen as unknown with a cost of $0$, as I said in my editorial.
•  » » » » » 2 months ago, # ^ |   0 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?
•  » » » » » » 2 months ago, # ^ |   0 Yes, the answer is the total cost minus the cost of the maximum spanning tree.
 » 2 months ago, # | ← Rev. 3 →   0
 » 2 months ago, # | ← Rev. 2 →   0 having done this problem really helped :Pthough 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 why using #include #include #include #include #include gives error error: 'std::tuple ' has incomplete type auto [h, i, j] = pq.top(); but #include works?? 
•  » » 2 months ago, # ^ |   0 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.
•  » » » 2 months ago, # ^ |   0 These are results from the contest judge, I tested these in the practice round, I guess some additional header is needed for structural binding.
•  » » » » 2 months ago, # ^ | ← Rev. 2 →   0 Fixed. Should include tuple instead of algorithm.
 » 2 months ago, # |   0 The time limits were huge
 » 2 months ago, # |   +6 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.
•  » » 2 months ago, # ^ |   0 My take, from what I can make of it so far : To recap, the goal is to break cycles of -1's at a minimal cost. This is done by finding the maximum spanning tree in a given array where A[i][j] = -1 and by restoring all other -1's at their given cost B[i][j] Maximum/Minimum spanning trees are implemented using Data Set Unions (DSU's) DSU's are implemented via 1D arrays. Here we have a 2D array so how do we bridge this gap? Each cell can be seen as the intersection of a (unique) combination of a row and column In this problem, each unknown value (i.e. A[i][j] == -1) uniquely corresponds to row (i) and column (j) Instead of thinking about this in 2D, consider flattening out the 2D array to a 1D (since DSU operates on 1D) Then, each cell, A[i][j], would become A[i*N + j] where N is the number of columns, and there would now only be one row (by definition since its 1D) A[i*N + j] is the equivalent of a DSU union of (i + N, j) So the overall mapping is: A[i][j] -> A[i*N + j] -> DSU union (i + N, j) (i + N, j) is the equivalent of connecting row (i + N) to column j So we are effectively drawing out two separate sets of Rows and Columns (given that they are all uniquely numbered now), and connecting across them during the merge. Note that the merge never connects one row to another row, or a column to another column, always a row to a column.
 » 2 months ago, # |   +3 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