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

Автор imstark007, история, 16 месяцев назад, По-английски

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.

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

»
16 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

atleast share problem link

»
16 месяцев назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

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