Блог пользователя rhezo

Автор rhezo, история, 8 лет назад, По-английски

How to solve BAT2-SPOJ?

My idea is that you need to find all increasing subsequences from left (1->n), and all increasing subsequences from right (n->1). Since n can be <=100, this will take 2^100 time, which is too slow. How can it be done efficiently? Or am I thinking in the wrong direction?

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
8 лет назад, # |
Rev. 3   Проголосовать: нравится +3 Проголосовать: не нравится

I think so. You must find length of max increasing subsequence in array B. Array B = A + A, where A is array that we were given. It can be done in O(nlog(n))

code
»
8 лет назад, # |
Rev. 2   Проголосовать: нравится +15 Проголосовать: не нравится

Clearly you don't have enough time to enumerate all those sequences, so that direction won't lead very far. Note that instead of thinking that Catwoman moves from right to left in an increasing sequence, the problem doesn't change if she moves from left to right in a decreasing sequence.

Why is this helpful? Because now Batman and Catwoman are moving in the same direction, which should let you use a really well-known technique to construct both of their paths at the same time.

»
7 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

we can see that its finding disjoint increasing subsequence and longest decreasing subsequence used simple DP

dp[pos][x][y] = the longest increasing subsequence and decreasing subsequence, using elements 1..pos, and the last index of longest increasing is x, while the last index of longest decreasing is y

then dp[pos][x][y] is the max of the following:

dp[pos+1][x][y] //not take the pos element
dp[pos+1][pos][y] + 1 //take the pos element as the next element in increasing (a[pos] > a[x])
dp[pos+1][x][pos] + 1 //take the pos element as the next element in decreasing (a[pos < a[y])
  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I just ACed the problem using your logic,thanks a lot... But I submitted a recursive DP solution, Can someone provide me a bottom-up table method DP of this problem,thanks a lot!!

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      Can you please share your code, with some comments. And I am not getting what will be the base case in recursion.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Sure , CODE

        Pardon me , the comments can be a little hard to read because of the indentation , but its still readable and I did my best explaining the DP in the code , If you dont get it , ask!

»
7 лет назад, # |
Rev. 3   Проголосовать: нравится -8 Проголосовать: не нравится

Found the error, fixed