Noobish_Monk's blog

By Noobish_Monk, history, 8 months ago, In English

Hi everyone!

This blog is totally not about asking to debug my code

On some recent div 1 rounds there were problems (this 2147F - Exchange Queries and that 2150C - Limited Edition Shop) which had one extra permutation in the input array. There were two given, but one of the permutations could be transformed to identidy. However, in order to do that, I need to permute other arrays accordingly. And that caused me a lot more trouble than I wish it did.

Usually I do something like this

void permute(vector<int> &a, const vector<int> &p) {
    int n = a.size();
    vector<int> tmp(n);
    for (int i = 0; i < n; i++)
        tmp[p[i]] = a[i];
    a = tmp;
}

but then a question arises "Why $$$tmp_{p_i} = a_i$$$ and not $$$tmp_i = a_{p_i}$$$?". And when do I use which variant?

In problem 2150C - Limited Edition Shop I also need to remake $$$b$$$ with a different rule, I can't simply call permute(v, a) and permute(b, a). I can kinda see why it's not working, but I don't see why it shouldn't work. If somebody knows some way to understand how to permute the array depending on the situation, please explain it in the comments.

P.S.: calling permute(p, p) is a bit funny because I give a const and a regular reference at the same time

  • Vote: I like it
  • +45
  • Vote: I do not like it

»
8 months ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

You can interpret a permutation as a function, then you can write the symbolic statement of $$$a = b * c$$$ or $$$a =b\cdot c^{-1}$$$ or similar. After having the matheamtical as expression in terms of functions, you can translate it into code easily. You can create two functions, one corresponding to matheamtical composition in right-handed order ( so ab means apply b first, then a) , and one corresponding to inverse.

How to decide which formulation? This is an extremely difficult question, and is no easier than generally how to write down the correct counting formula in a counting problem, for example.