Tanmoy1228's blog

By Tanmoy1228, history, 9 years ago, In English

can anyone give me any idea for this problem.

problem: Shil and Wave seqeunce

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

»
9 years ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

dp[i][0]=number of wave seq where a[i] is the last element and the one before him is lower(<)

dp[i][1]=number of wave seq where a[i] is the last element and the one before him is bigger(>)

Now to get the answer you maintain 2 BITs (for < and >).For every BIT you update for an element a[j] at position a[j] with dp[j][0](for the first BIT) and dp[j][1] (for second). To get the answer for dp[j][0] you query the second BIT(>) in interval [1,a[j]-1] and for dp[j][1] you query first BIT(<) in interval [a[j]+1,max_val].

Actually for a query,you want to link a[j] to all sequence from before that respect some conditions,that's why for dp[j][0] we want to link a[j] with an element smaller [1,a[j]-1] and that has been linked to a bigger element (we do query on BIT >).

Also take care that you have to add 1 to dp[j][0] and dp[j][1] as a[j] single can do a wave seq(in theory,for making sequences of length>1,you have to start with 1).

The answer is in the sum of all dp[i][0/1] — 1. Hope I helped.