SiddhyaRushLittlelordOP's blog

By SiddhyaRushLittlelordOP, history, 10 months 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 :-)

»
10 months ago, # |
  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

  • »
    »
    10 months ago, # ^ |
    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.

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      they should talk in such order that the answer is maximum: example 12345 if 1 talk with 2 answer += g12 + g21, after 3 talk with 4 answer += g34 + g43, after 1 gone to shower, now remained 2345 in queue,

      now: 2 talks with 3 and 4 talks with 5, after 2 gone to shower , now remained 345

      now: 3 talks with 4 , after 3 gone to shower and remained 45

      4 talks 5 , after 4 gone shower and remained 5

      if i talks j answer += g[i][j] + g[j][i]

    • »
      »
      »
      10 months ago, # ^ |
        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.