SiddhyaRushLittlelordOP's blog

By SiddhyaRushLittlelordOP, history, 3 years ago, In English

Note: I'm unware whether I can ask question here. LMK if this post isn't suitable for guidelines. I'll delete it. ;-)

Hello, I'm having confusion about understanding the intuition behind the problem 431B — Shower Line.

I've read the problem statement and editorial it suggested this solution

Can anyone who has solved this problem please give me hint on how to come up with these arr[i][j] numbers in the loop

 do
    {
        //01234
        tmp = g[p[0]][p[1]] + g[p[1]][p[0]];
        tmp += g[p[2]][p[3]] + g[p[3]][p[2]];
        
        //1234
        tmp += g[p[1]][p[2]] + g[p[2]][p[1]];
        tmp += g[p[3]][p[4]] + g[p[4]][p[3]];
        
        //234
        tmp += g[p[2]][p[3]] + g[p[3]][p[2]];
        
        //34
        tmp += g[p[3]][p[4]] + g[p[4]][p[3]];
        
        if(tmp > ans)
        {
            ans = tmp;
            for(int i = 0 ; i < n ; ++i)
                pans[i] = p[i];
        }
    }
    while(next_permutation(p, p+n));

Thanks in advance :-)

| Write comment?
»
3 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hi, You look one by one all cases(12345, 13245, 14235, ... ) with next_permutation(), pls say me what are you not understand

  • »
    »
    3 years ago, hide # ^ |
    ← Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    Isa_m The input is coming in this format:

    0 0 0 0 9
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    7 0 0 0 0
    

    I am unable to understand how the 1D cases(12345, 13245, 14235, ... ) are getting interpreted here in 2d matrix. I also could not found any explanation online.

    • »
      »
      »
      3 years ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      The 1D matrix has the current permutation of the numbers 1 to 5. Now inside g[i][j] 2D array, you are using the elements of the permutation as the index.

      for example, p = [5,3,4,1,2]

      then g[p[0]][p[3]] is actually g[5][1], as p[0] = 5 and p[3] = 1.