imstark007's blog

By imstark007, history, 16 months ago, In English

Problem Link

Can anyone please explain how to solve this question by DP.

I have read the tutorial of this problem but still can't get the intuition.

Or please suggest similar question question to get the intuition.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
16 months ago, # |
  Vote: I like it 0 Vote: I do not like it

atleast share problem link

»
16 months ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Okay so you can store dp[i] as the number of max balls that can be removed till that place eg 4 6 6 8 6 4 dp[0] = 0 (base) dp[1] = 0 (just one 4 so nothing happens) dp[2] = 0 (again nothing happens) dp[3] = 2 (both 6) dp[4] = 2 dp[5] = 4 dp[6] = 6

so basically for calculating dp[5] suppose for value 6 i find previous 6 6 was present at 2 and 3 index so ans for dp[5] = max(dp[1] + (5-2)+1) and dp[2] + (5-3)+1 and ofc dp[5] = max(dp[5], dp[4])

so i will have to calculate for all possible occurences of 6 before this will give n2 solution which can be optimized using priority queue now you know the ans is determined by dp[index] and diff in index try putting a custom comparator for your priorty queue such that the first value you pop will always be the ans you will get max dp with

heres my solution https://mirror.codeforces.com/contest/1842/submission/217094157