Skip to main content
  1. Editorial/

Codeforces Round 970 Discussion stream (with hints)

·753 words·4 mins
aryanc403
Author
aryanc403
Asia West Champion, ICPC World Finals 2021 | CodeChef Snackdown, World Finalist 2021
Table of Contents

You can join in if something in this blog is unclear or there are more questions.

A. Sakurako’s Exam
#

Idea
  • No of ones must be even.
  • If no of ones is zero, then no of twos must be even.

These are necessary and sufficient conditions.

Video Editorial

My submission — 279068120

B. Square or Not
#

Hint 1

If it is a square matrix, then N must be a perfect square.

Hint 2

If N is a perfect square, then R=C=Sqrt(N).

Hint 3

Now, we can create R*C beautiful matrix, and then create required string. Then check if it is equal to given string.

Video Editorial

My submission — 279071329

C. Longest Good Array
#

Hint 1

Lets try to create array greedily.

Hint 2

$A_1=L$,

$A_i-A_{i-1}=i-1$ for all $2 \le i$,

In general, after simplification,

$A_i=L+(i-1) \cdot i/2$ for all $2 \le i$.

Hint 3

Longest array length depends only on $R-L$. We need the largest $N$ such that $N \cdot (N-1)/2 \le R-L$.

Video Editorial

My submission — 279077055

D. Sakurako’s Hobby
#

Hint 1

Permutation cycle

Hint 2

Create a graph with edge $i \to p_i$, and notice that each connected component is a cycle. For each connected components count the no of black nodes, and then update this count for all nodes.

Video Editorial

My submission — 279082914

E. Alternating String
#

Hint 1

Solve for N = even first.

Hint 2

We should look at chars at odd index and even index. Greedily chose the one that appears maximum no of times, and replace all the other chars.

Hint 3

For N = odd, we can iterate on index i that needs to be deleted. Chars at odd index before index i we will remain at odd index, and chars at even index after i will move to odd index.

Hint 4

One can either use sliding window or prefix sum to find new frequecies at odd and even index after deleting char at index i.

Video Editorial

My submission — 279127640

F. Sakurako’s Box
#

Hint 1

We should find sum of product of all pairs, then we can divide by $N \cdot (N-1)/2$ for expected value.

Hint 2

$\sum_{i=1}^{n} \sum_{j=1}^{i-1} a_i \cdot a_j$

$= \sum_{i=1}^{n} a_i \cdot (\sum_{j=1}^{i-1} a_j)$

Hint 3

You can now iterate on index i, and maintain a running sum of all the index before it. Use this sum and $a_i$ to find sum of product of all pairs.

Video Editorial

My submission — 279089762

G. Sakurako’s Task
#

Hint 1

Solve for $N=2$ first.

Hint 2

Forget about $K$, and try to find smallest two positive nos you can have in the array.

Hint 3

Use only $a_i = a_i - a_j$ operation.

Hint 4

Let $u \ge v$. If we repeatedly doing $u = u - v$, as long as we can. We will end up with $u = u \bmod v$.

This algorithm now sounds familiar?

Hint 5

Euclidean algorithm

Hint 6

The two nos you will end up will be $\gcd(a_1,a_2)$ and $\gcd(a_1,a_2)$, then we can change them to $0$ and $\gcd(a_1,a_2)$.

Hint 7

Now, we can generalise this, and say we will end up with $n$ times $\gcd(a_1,a_2,…,a_n)$.

Hint 8

We can change one of these to $0$, and for remaining we can do, $a_i = a_i + a_j$ operation to obtain first $N-1$ multiples of gcd. Then check which is the kth smallest missing no.

Pitfall

$a_i = a_i + a_j$, this operation only changes the value which is larger. So if we change any no to $0$, we can never increase it.

This was the reason why constraints were change to $1 \le a_i$.

Thanks neal and Dominater069 for flagging earlier hints I had were wrong.

Video Editorial

My submission — 279141273

H. Sakurako’s Test
#

Hint 1

Because we want smallest median, we should do operation as long as we can. We will end up with $a_i = a_i \bmod x$.

Hint 2

Binary search for the median, and try to find how many nos can be $\le \text{mid}$.

Hint 3

To get this count, we need count of nos in range $[j \cdot x, j \cdot x+\text{mid}]$. We can find this sum in $O(N/X)$ using prefix sum. With overall time complexity $O(N/X) \cdot \log X$.

Hint 4

Queries can contain repeated $X$, but memorise the result for so that you do not have to find it again. This will lead to $N \cdot \log^2 N$ time complexity.

Video Editorial

My submission — 279103861