Hello everyone!

I want to invite you to participate in March Clash at HackerEarth. Contest is scheduled on March, 26. Contest duration is 24 hours, so there should be some comfortable time for every timezone :)

There will be six tasks in a problemset. Five of them are standard algorithmic problems with partial solutions allowed — you get points for every test that your solution passed. And the last task is an approximate problem — your score for this task depends on how good your solution is comparing to current best solution.

amd is author of this problemset. You may check some of his previous contests to ensure that he's always providing contestants with interesting tasks :)

I was working on this contest as a tester, and I enjoyed solving given problems a lot :) I hope that several tasks will be not too hard for beginners *(don't give up and show your best with partial scoring — even very naive solution may give you some decent points; and 24 hours should be enough for you to read all problems and find some tasks which you can solve)* while some tasks are challenging enough to make this contest interesting for more experienced contestants. Even if you think that classic part of problemset is easy — go on, try to beat other contestants on approximate problem :) shef_2318 worked on this contest as translator — you will be provided with problem statements in English and Russian. Also I want to thank belowthebelt for handling all technical aspects of contest preparation.

As usual, here is one more reason for you to participate in this contest:

**Top5 of leaderboard will also receive some nice prizes:**

- $100 Amazon gift card + HackerEarth T-shirt
- $80 Amazon gift card + HackerEarth T-shirt
- $50 Amazon gift card + HackerEarth T-shirt
- HackerEarth T-shirt
- HackerEarth T-shirt

Good luck to everybody — I hope to see you at the scoreboard :)

*Upd. Contest has ended :) Thanks to everyone for participating :) Congratulations to winners:*

1) Carsten Eilks

2) ffao

3) LayCurse

5) mugurelionut

*All solutions are available, also for all classic problems editorials and solutions by setter and tester have been published.*

One hour to go for the contest. Good luck. :)

Did you consider to include sample explanations? That would help to make P4 more tolerable.

In the approximate problem, I think HackerEarth could have saved some considerable server spamming on my part if the generation method for the test cases was disclosed.

I agree. Please don't use approximate problems where the test case generation method is not disclosed. It makes it almost impossible to test our solutions locally. This problem in particular had a large space of inputs and we were supposed to simply guess how the test cases were generated from such a large pool of possibilities? In particular, some approaches which worked really well on my local test cases made almost no difference or scored worse on the official test cases. In some of the recent Hackerearth contests this issue did not show up anymore, so I thought it was finally solved, but now it's back again. I guess it all depends on the experience of the problem setter, but I think the tester should also push for disclosing the test case generation method.

Can anyone explain the solution to P2 (The Great KL)?

Sqrt decomposition.

sqrt(N).Use standard algorithm (2 DFSs) for finding perimeter of a tree in

O(N).sqrt(N)Choose a root, compute levels and ladders, Then check the distance of each pair in so the total complexity would be , where

n_{i}is the size of this group. (You can also use Tarjan LCA to avoid extra log)Ah nice, I thought it was going to be some complicated algorithm. So the solution runs in .

In fact when we use the threshold for big/small to be it is which is better. But still when we use Tarjan for LCA it is plain .

Not necessary. Consider this: We have a tree and some vertices are red and others are blue. We know the two endpoints of a longest path between any two red vertices. We paint a blue vertex with red color and we want to update this longest path.

Lemma: One of pathes between these three vertices (endpoints of previous longest path and new vertex) is a longest path now.

Converting the original problem to this trick is easy.

NlogN solution.

For each color keep track of the 2 farthest nodes and whenever u find a node whose distance to one of the 2 farthest nodes of that color is more than the distance between those 2 nodes , update the farthest pair of nodes for that color.

Code

I solved it with centroid decomposition.

There are two cases:

If the diameter pass through the centroid, then we only need the deepest node of every color in each of the branchs

Otherwise, calculate recursively for each of the subtrees that are hanging on the centroid

Can anyone explain the solution of 3. problem?

Suppose that we fix some subset of

f's, sayf_{s1},f_{s2}, ...,f_{sk}, and we want to choose some permutationpsuch that alla[i] +p[i] belongs to {s_{1},s_{2}, ...,s_{k}}.In other words

p[i] can be equaljiff .It is a matching problem, between the domain of

pand its counterdomain. SayD= {0, 1, ...,n- 1} andC= {0, 1, ...,n- 1}, and the edge betweeniandjexists iffp[i] can be equal toj.Now let us go back to the original problem. We sort

f, sayf_{s1}≤f_{s2}≤ ... and fork= 1, 2, ... we check if we can find a permutationpfor the subset {f_{s1},f_{s2}, ...,f_{sk}}. If so then the answer to the original problem isf_{sk}.Binary Search on answer

We want to check an answer <= 'X' is possible or not.

Lets make a bipartite graphs with 'N' nodes from [0 , n — 1] on each side.

Now the weight of edge from i on left side to j on right side will be f[i + a[j]]. if this weight is less than 'X' then add this edge to list of edges.

Now find maximum bipartite matching.

If matching == 'N' then return true else return false.

Code

That is...

Solution to P4 (the great cyrus) ?

Consider that we had to remove edges instead of Nodes. In that case it is a standard problem of finding the min-cut which is equal to the Max-Flow.

Now we need to remove nodes instead of edges , so we keep 2 copies of each node say V1 and V2. Now for every edge (U -> V) in input , add edges in our graph from U2 to V1 and since its undirected , edge from V2 to U1 as well , give it weight infinity. And then for all nodes from 1 to N , add an edge from V1 to V2 with weight = the cost of removing that node. And then just find the maxflow from A1 to B2. Code

Dude, slow down. The main idea was inside the min-cut part. What should be the cost to remove an edge?

I used

ai*X+biwhereXis a big number. However I got only 91.2 points :(Maybe your X was too big or not big enough.

How I could know my approximation problem results in upsolving? Found bug five minutes after the end of the contest, and want to know how much it cost for me.

+1. It seems that once the contest ends, the scores for the approximation problem are not shown anymore (neither for old submissions, nor for new ones). You only get if the solution was Accepted on each test case or not, but not the score which measures the quality of the solution on each test case (which is the most important thing).

@organizers: Can you please fix this issue with the approximation problem? It's really annoying (both for upsolving and for looking at the best submissions of the other competitors).