How do you understand, how to permute an array?

Revision en1, by Noobish_Monk, 2025-09-28 22:47:51

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

Tags permutations

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Noobish_Monk 2025-09-28 22:47:51 1286 Initial revision (published)