Solution of Codeforces Round #831 (Div. 1 + Div. 2) || Tutorial of Codeforces Round #831 (Div. 1 + Div. 2)

Solution of Codeforces Round #831 (Div. 1 + Div. 2) || Tutorial of Codeforces Round #831 (Div. 1 + Div. 2)


Codeforces Round #831 (Div. 1 + Div. 2, based on COMPFEST 14 Final) Editorial


1740A. Factorise N+M

Author: Pyqe
Developer: Pyqe

Tutorial

1740A - Factorise N+M

There are multiple solutions for this problem. We will discuss two of them.

One solution is to choose m=n. This always guarantees that m is prime, because n is always prime. And we can see that n+m=n+n=2n, which is always not prime, because n>1 always holds.

Another solution is to choose m=7. If n is odd, then n+m will be an even number greater than 2 and therefore not prime. Otherwise n is even. The only even number prime number is 2 and it can be verified that 2+7=9 is not a prime number.

Time complexity for each test case: O(1)

1740B. Jumbo Extra Cheese 2

Author: Pyqe
Developer: errorgornPyqe

Tutorial

1740B - Jumbo Extra Cheese 2

Consider arranging the rectangles such that the i-th rectangle from the left has width ai and height bi. The perimeter of the connected figure will be 2(a1+a2++an)+b1+|b1b2|+|b2b3|++|bn1bn|+bn.

Notice that if we sort the rectangles based on bi, the cost would become 2(a1+a2++an+bn), which is the minimum possible perimeter if we cannot rotate the rectangles. This means, if we have a fixed configuration of the orientations of the rectangles, we can always permute the rectangles such that the perimeter is 2(a1+a2++an+max(b)).

We need to find an orientation configuration that results in the minimum value of 2(a1+a2++an+max(b)). We can observe that it is always more optimal to orient them such that aibi.

Therefore, the solution is to orient the rectangles to satisfy aibi, then the answer is 2(a1+a2++an+max(b)).

Time complexity for each test case: O(n)

1740C. Bricks and Bags

Author: Pyqe
Developer: Pyqe

Tutorial

1740C - Bricks and Bags

Firstly, sort a. From now on, we will always refer to the sorted array a.

Let pj be the index of the brick taken by Bu Dengklek from bag j. We can obtain that Bu Dengklek will always take the bricks such that the configuration of p1,p2,p3 is in the form of one of the following:

  1. p1<p2<p3
  2. p1>p2>p3
  3. p2<min(p1,p3) and min(p1,p3)=p2+1
  4. p2>max(p1,p3) and max(p1,p3)=p21

In fact, Pak Chanek is always able to force it such that the bricks taken by Bu Dengklek form any configuration of p1,p2,p3 that follows one of the constraints above. Therefore, we just need to find the values of p1,p2,p3 satisfying the constraints above such that |ap1ap2|+|ap2ap3| is maximised.

To maximise the final score, we can see that it is always more optimal to either use the third or the fourth case for p1,p2,p3. For the third case, it is always more optimal to maximise max(p1,p3), so we should set max(p1,p3)=n. A similar logic can be used for the fourth case to see that we should set min(p1,p3)=1.

Therefore, the maximum final score is the maximum of these two cases:

  • (aiai1)+(aia1) with 3in.
  • (ai+1ai)+(anai) with 1in2.

Time complexity for each test case: O(nlogn)

1740D. Knowledge Cards

Author: Nyse
Developer: steven.novaryo

Tutorial

1740D - Knowledge Cards

Let card c be the card with number c written on it. Notice that we must put the cards into the stack in (n,m) in order from card k, card k1, card k2, and so on until card 1.

The key observation for this problem is that, under given constraints, if we ignore cells (1,1) and (n,m) we can always move any card to any location if there is at least one empty cell. We can obtain this by considering the fact that an empty cell can "move" to any cell and can be used to rotate any 2×2 square of cells. Because 3n,m, if we ignore cells (1,1) and (n,m), each cell is a part of a 2×2 square.

Therefore we can iterate the card in the stack (1,1) from top to bottom. While iterating, one should maintain all of the cards in the board that are not in cell (1,1) or (n,m). We define those group of cards as active cards. Each time we put a move a card out of (1,1), we add the card to the active cards. Before finishing the iteration we will try to move one or more cards from the active cards to (n,m) as long as there is a card we can move. We can maintain the active cards with a priority queue or a set.

The active cards can be moved around freely if and only if there is at least one empty cell in the grid if we ignore (1,1) and (n,m). Therefore, to check whether or not we can solve the puzzle, we just need to check whether or not there exists a moment where the number of active cards exceed nm3.

Time complexity for each test case: O(klogk)

1740E. Hanging Hearts

Author: Pyqe
Developer: Zappeko

Tutorial

1740E - Hanging Hearts

We define card x to be dangling onto card y if there exists a sequence [v1,v2,,vk] such that v1=xvk=y, and pvi=vi+1 for all 1i<k. We also let dist(x,y)=k explained above. Let Di be the set of cards such that jDi if and only if card j dangles onto card i.

Let wi be the number on card i when it is about to get removed. To remove card i, we must initially remove all cards j that are dangling onto card i. Thus, we can see that wi=min(ai,minjDi(aj)). Hence, it does not break the optimality of a solution if ai>maxjDi(aj).

In the optimal configuration, the elements of the longest non-decreasing subsequence in S comes from a sequence of cards that resembles the following:

  • It consists of several sequences of cards in the form of [v1,v2,,vk] such that pvi=vi+1 for all 1i<k.
  • For any pair of cards from two different sequences, one must not be dangling onto the other.

We will use dynamic programming on tree. Let dp[i] denote the length of the longest non-decreasing subsequence created from only doing operations to card i and all other cards dangling onto card i. Then, we have two cases:

  • If card i is used in the longest non-decreasing subsequence, then the maximum answer is the maximum value of dist(j,i) for all jDi.
  • If card i is not used in the longest non-decreasing subsequence, then the maximum answer is the sum of dp values of all cards j directly hanging onto i (i.e. pj=i).

Time complexity: O(n)

1740F. Conditional Mix

Author: Pyqe
Developer: errorgorn

Tutorial

1740F - Conditional Mix

Let cnti denote the number of occurrences of element i in array a.

We can obtain that a multiset M of size nM1,M2,,Mn where M1M2Mn0 is good if and only if i=1nMi=n and i=1kMii=1nmin(k,cnti) for every 1kn.

We proceed with formulating a dynamic programming solution. Let dp[pos][sum][last] denote that we have picked a prefix P1,P2,,Ppos such that i=1posPi=sum and Ppos=last. Our transition is going from dp[pos][sum][last] to dp[pos+1][sum+x][x] for all xlast, this can be easily handled using prefix sums. However, we still have O(n3) states.

We can cut down the number of states by noting that ni=1posPilastpos. That is, for each value of pos, there are only npos values of last. Since n1+n2+n3++nnnlogn, the number of states is bounded by O(n2logn).

Time complexity: O(n2logn)

1740G. Dangerous Laser Power

Author: Pyqe
Developer: Pyqe

Tutorial

1740G - Dangerous Laser Power

Claim: There is a construction where all portals are good.

We will proceed with the construction.

The key observation of the problem is that for a portal (i,j) to consume energy, the laser must not enter a portal with a strength greater than or equal to si,j before going through portal (i,j).

Let's say we already have the type settings of all lasers. Consider all possible lasers that at some point enters a certain face of portal (i,j). We can find the paths of those lasers before entering portal (i,j) by backtracking in that direction from portal (i,j).

Consider the first portal in that backtracking path that has a strength greater than or equal to si,j. Notice that all lasers that starts from portals that are located after that portal in the backtracking path must go through that portal before entering portal (i,j). Therefore, those lasers cannot make portal (i,j) use energy, which means they can be ignored.

Using the observation above, we can see that we must only backtrack until we find a portal with a strength greater than or equal to si,j. Observe that to get the path up to that point, the type settings we should know are only the type settings of portals with strengths smaller than si,j.

Therefore, the construction can be generated by calculating the type settings of the portals from the smallest to the largest strengths. In each iteration, we use the type settings of the previous portals to find the backtracking paths of the current portal. If we already know the 4 backtracking paths for each of the 4 directions of that portal, we can find the total energy that portal will consume in the end, which means we can find the type setting for that portal that makes it a good portal.

The naive implementation of this has a time complexity of O(n2m2). Notice that each backtracking path of the portals are just the merging of smaller paths. We can maintain a disjoint set data structure to handle this while maintaining the essential values needed in the calculation of the total energy consumed for each path.

Time complexity: O(nm)

1740H. MEX Tree Manipulation

Author: steven.novaryo
Developer: steven.novaryoPyqe

Tutorial

1740H - MEX Tree Manipulation

Define MEXi(A) of a certain array A as the i-th smallest non-negative integer that does not belong to the array.

Let's solve a simpler problem. Imagine that you are given an array A, you are asked to append a new element x to the array, and you need to calculate the MEX of the array after the operation. It is easy to see that the new value of MEX is either MEX1(A) if xMEX1(A) or MEX2(A) if x=MEX1(A) where A is the array before the operation.

Let (x,y,z) define a relationship between input and output which means:

  • If the input is not x, then the output is y.
  • If the input is x, then the output is z.

This means the relationship between the new element to be appended to A and the new value of MEX can be expressed as (MEX1(A),MEX1(A),MEX2(A)). Define that triple as MEXtuple(A).

Define the heavy child of vertex u as the child with the largest subtree size. If there are children with the same subtree size, choose one of them. Let's call each child other than the heavy child as a light child. In particular, the edge that connects a vertex to its heavy child is called a heavy edge.

Let Cu be the array that contains the values of the light children of vertex u. We can see that if we maintain MEXtuple(Cu) we can get the relationship between the value of the heavy child and the value of vertex u.

Let's consider a path in the tree that traverses through parents while only using heavy edges. Consider the relationship between the value of the vertex at the beginning of the path and the end of the path. Let's calculate the MEXtuple for each vertex in the path other than the first one. We can see that we can get the relationship between the vertex in the beginning of the path and the end of the path using those MEXtuple values. In fact, that relationship can also be expressed as a triple (x,y,z) defined above.

Let's decompose the tree into chains of heavy edges. For each chain, construct a segment tree with each node in the segment tree calculating the relationship triple (x,y,z) for a segment of the chain.

Suppose we want to add a new vertex. The vertices that have their values changed are the ones that lie on the path from the new vertex to the root. Observe that that path only traverses through at most O(logQ) different heavy edge chains. For each chain, we can get the value of the vertex at the end of the chain by doing a point update on the chain's segment tree. When we move between two different chains, we update the Cu for a vertex and recalculate it's MEXtuple in O(logQ) time complexity using a set or another segment tree. We repeat this process until we reach the root. We can calculate that the total complexity of a single query is O(log2Q).

To get the sum of the values of the entire tree after an operation, we can store two new variables in each node of the segment tree of each chain that represents the sum of the chain segment for the two possible cases of the input.

Time complexity: O(Qlog2Q)

1740I. Arranging Crystal Balls

Author: NeoZap
Developer: errorgornPyqe

Tutorial

1740I - Arranging Crystal Balls

Let's simplify the problem such that the only possible operation for each k chosen statues is ai:=(ai1)modm.

To solve this, we first define ci as the number of times we do that operation for the statues i,(i1)modn,(i2)modn,(i3)modn,,(ik+1)modn. We can see that in the optimal configuration, 0cim1 must hold. For it to make each element of a to be 0(ci+c(i+1)modn+c(i+2)modn++c(i+k1)modn)modm=ai must hold. The number of operations is equal to c0+c1+c2++cn1.

In order to extend to the original problem, we can see that for a given array c, the number of operations that can be made to be equal to i=0n1min(ci,mci). So we need to find an array c satisfying the conditions above such that i=0n1min(ci,mci) is as small as possible.

We can see that a(i+1)modnai=c(i+k)modnci. That means, if we have determined the value of ci, the value of c(i+k)modn is forced. If we form an edge for each pair of indices (i,(i+k)modn), we will get GCD(n,k) connected components. This means if we have determined the value of one vertex for each connected component, the values of the entire array c are forced. However, we must also consider possibility that at least one connected component cannot have assigned values that satisfy all of its edges. In that case, it is impossible to finish the objective of the problem.

Define d=GCD(n,k). For each ci, we can find the difference relationship between it and cimodd. Therefore, to answer the problem, we must find the values of c0,c1,c2,,cd1 such that c0+c1+c2++ck1=a0 and if all the other values of c are calculated, i=0n1min(ci,mci) is as small as possible.

For the c0+c1+c2++ck1=a0 condition, using simple modular algebra, we can calculate one or more values b1,b2,b3, such that c0+c1+c2++ck1=a0 if and only if c0+c1+c2++cd1 is equal to one of the values of b.

Let's consider the connected component ci,ci+d,ci+2d,. It must have nd elements. Define fi(x) as the sum of min(cj,mcj) for all elements of cj in that connected component if ci=x. We can easily calculate all values of fi(x) for all connected components in O(nm).

After this, we must the minimum value of f0(c0)+f1(c1)+f2(c2)++fd1(cd1) such that c0+c1+c2++cd1 is equal to one of the values of b. This can be solved using modular knapsack dynamic programming, but the time complexity is O(nm2). We must optimise it further.

Consider the contribution of a single element cj to the values of fi(x) of its connected component. Let's say the difference relationship is that cicj=w must hold. Notice that:

  • It adds (xw)modm to all values of fi(x) for each x going up modularly from w to (w+m2)modm
  • It adds (wx)modm to all values of fi(x) for each x going up modularly from (w+m2)modm to w.

We can see it as "changing slope" at most three times, namely in x=wx=(w+m2)modm, and x=(w+m2)modm.

This means, if we add up all contributions of each element, the number of points the slope changes in one connected component is at most 3nd. We will call these points as special points.

Consider an array c0,c1,c2,,cd1 satisfying the condition that c0+c1+c2++cd1 is equal to one of the values of b. Consider the case if there exists at least two different indices i such that the value of ci is not a special point in fi. Suppose the indices are i1 and i2. Since both of them are not special points, the slope between (ci11)modm and ci1 is the same as the slope between ci1 and (ci1+1)modm. The same is true for i2. Let's say z1 is the increase in value of the final answer if we do ci1:=(ci1+1)modm and ci2:=(ci21)modm, and z2 is the increase in value of the final answer if we do ci1:=(ci11)modm and ci2:=(ci2+1)modm. From the observation above, we can see that z1=z2, which means one of them is non-positive. This means, if this is the case, we can always change the array c into another array with an equal or smaller final answer.

This means, we can always do that operation to get an array c such that there is at most one non-special point. Therefore, we can just consider those cases to find the minimum answer.

We can solve this using divide and conquer. We first call the recursive function dnc(0,d1). The moment we call the function dnc(l,r), we have made the modular knapsack dynamic programming array if we have only considered the indices i with i<l or r<i and only considering their special points. dnc(l,r) recurses into dnc(l,l+r2) and dnc(l+r2+1,r). Once l=r, we will consider that index as the one that can have a non-special point, then we can just iterate the array b and take the answer from the knapsack array.

The depth of the recursion is at most O(logd). Each depth has insertions of O(d) indices to the knapsack. Because each connected component only has O(nd) special points, each depth does O(n) dynamic programming transitions with each transition having O(m) time complexity.

Time complexity: O(nmlog(GCD(n,k)))

Comments

Popular posts from this blog

Whiteboarding Interviews

Version Control with Git: A Beginner’s Guide

Callback function in JavaScript