MATTEG - Editorial

Hi, it seems you read the input wrong. It was stated that we fisrt give all the x coordinates and then all the y coordinates. But you are reading numbers in pairs. Rest seems fine to me.

damn,thank you!

Yeah, it isn’t showing there.Its not showing an AC/TLE/RTE eihter. But I can see the WA on my submissions list. Maybe thats why it was judged wrongly. Please have a check.

@pk301, can we clarify a bit more. Also, we are not skipping any possible states. All the required states (which will lead to answer) are captured into the solution.

sorry but i didn’t understand the third subtask part …can u please explain a little more !

@utkarshg_1998, yes by first intuition , your approach looks correct. But think in terms of masking. For example, when writing Travelling salesman problem solution, you see that that each point you have k edges to chose from at a vertex. but still the complexity is not like you mentioned. n * 5^n, signifies that first states would capture which one was chosen and the next which possible directions.

I think it will be more clear from tester’s solution his approach is totally based on this. From his code it will be clear that all possible states are being visited from it. I hope it make things somewhat more clear.

5^N basically comes from the fact that at each location, we have 5 choices to go with i.e stay at same location or go to one of the 4 possible directions. I recommend you to write solution for 40 points and print the {x, y, mask} tuples visited in recursion and check that all states are not being visited. For help, you can see author or mine code with the debugging statement removed. I hope this makes it more clear.

@venture_walk, yes it is necessary as explained in the editorial regarding shortcomings of map and unordered_map. This is not required if you solve using tester’s approach as x & y coordinate are not required to be stored now and it will easily fit in memory. Also remember, using large memory in program also increases runtime as well as cache effects reduce.

Thank you so much for replying.

And what was the significance of multiplying with 2^N in (a∗R+b)∗2^N+mask, again? :sweat_smile:

And I also came to know that boost::unordered_map is slower than std::unordered_map. :confused:

@venture_walk It was an encoding of the state in the dp. The state is (x, y, mask) where x and y are the coordinates of the cell and mask is the configuration of tel-pairs. Now, we can uniquely identify (x, y) as a single value, x * (N - 1) + y. This is a value that uses 20 bits. If we shift it by N bits then we can store the mask as the last N bits.

It is equivalent to storing tuples (x, y, mask) but much faster.

boost::something is usually better than std::something but boost is not allowed in online judges.

Oh! I see the awesomeness : “If we shift it by N bits then we can store the mask as the last N bits.”

Thank you so much :slight_smile:

Exactly!

You’re welcome!

@hrushikesh_t, you are not clearing the dp for every test case. Also, this solution will still tle as you are using unordered_map. See editorial for more details. You need to implement your own hash table or else use tester’s approach.

@givingmybest,changing from 0->2^N-1 to 2^n-1->0 doesn’t make any difference. Also, in general contests, you will not get memory limit as 2GB. So, it is preferred you learn new techniques here. It may be possible that with such large memory, cache effects play major role and switching parameters like [x][y] to [y][x] may lead to different running times on same test cases and will depend on test cases as well. So, I would recommend to avoid this solution.

@john_smith_3, yes your observation is correct. The solution will be updated soon as well as the editorial. Thanks for pointing it out.

@john_smith_3 You are right! Thanks for finding the bug!

@taran_1407, I would first advise you to read about travelling salesamn problem solution using bitmasking. The idea is similar here. The mask (base 5) contains the best possible solution inside it considering all permutations. When adding a new teleportation pair into current mask, we just need to check whether it will lead to valid move or not and add the node value (if move is valid) to the value stored in mask.

For this also, I suggest you to first try to write recursive solution, where you will see the permutation you are talking about is clearly taken care of. Then, you may read and understand the tester’s solution.