hars123's blog

By hars123, history, 5 months ago, In English,

I have tried this problem a lot and also figured out the states. I am trying to build up a 2D dp table dp[i][j] where first i-1 signs are satisfied and last number in the sequence is j. can someone help me in finding out the transitions for these states and solve this problem in O(N^2). If someone could suggest a top-down approach it would be really helpful as i generally find it difficult to find transitions using bottom-up approach.

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

»
5 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Are you sure your DP states are correct? My thoughts are DP[i][j] denotes number of permutation for first i'th signs such that there are j numbers smaller than current number in the permutation obtained till now and then I think we can use prefix sum. Can someone please explain the correct solution?

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

    I am also giving it a try and trying to arrive at a solution.I will also be thankful to someone who could guide me

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

      I missed it. There is already explaination here https://codeforces.com/blog/entry/64250

      • »
        »
        »
        »
        5 months ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        I have gone through the comment by Rudy358 but when i am trying to build the dp table from it its just not coming out correctly.It feels like i still have not got the right intuition about the solution.It would be helpful if you could explain me the idea behind the solution.

        • »
          »
          »
          »
          »
          5 months ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          DP[i][j] = no. Of permutat**n we can get of length i such that j numbers are smaller than value at position i. Then consider two case :

          If p[i] > p[i-1] Then dp[i][j] is sum of all dp[i-1][k] where k < j. Since there will be at most j-1 smaller number than p[i-1]

          If p[i] < p[i-1] Then dp[i][j] is sum of all dp[i-1][k] where k >= j. Since there will be atleast j smaller number than p[i-1]

          Now this solution will be O(n**3) you can speed it to O(n**2) if you store prefix sum for each layer of i.

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Clearly explained by errichto in this video.