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

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

During the Educational CF Round 128 while solving problem C, I stumbled upon a cheeky DP based solution to the problem.

Problem: 1997C - Even Positions

(it doesn't even have a dp tag)

for those who're too lazy to click on the link, in short the problem says that.

we're given a sequence a Bracket sequence which was originally a valid bracket sequence but now every odd position element is lost and we have the remaining sequence (eg: _(_)_)). now we need to find the minimum cost between all the possible valid sequences where the cost of a sequence is summation of the distance between each opening and its corresponding closing bracket (eg: for _(_)_) a possible valid sequence can be (())() and its cost would be $$$3 + 1 + 1 = 5$$$).

Now given the incomplete bracket sequence we need to find the minimum possible cost among all the possible valid sequences.

Constraints are so that its always possible to construct a valid sequence.

Solution I found:

we can say that $$$dp[i] = \text{minimum cost if we just consider the suffix from i to n}$$$

the base case will be $$$dp[n] = 1$$$ as last element will always be ) and the only valid answer for that is always () which gives 1 as the minimum cost. we can clearly see this in the figure below.

Image to show that only the top left combination works which gives the answer for base case as 1

now the transition from one state to next state will be

$$$dp[i] = dp[i + 1] + 1$$$ (if we stumble upon ))

or $$$dp[i] = dp[i + 1] + 3$$$ (if we stumble upon ().

we can just create our answer all the way to $$$i = 1$$$ and our final solution will be at $$$dp[1]$$$ (PS: we're considering 1 based indexing here)

now why does it work ? let's look at some bigger cases.

We can observe that no matter which type of bracket we get we can always create smaller brackets whose cost will be 1.

if we get ) we just create a smaller bracket but if we get ( we still create a smaller bracket of cost 1 but we push it inside a bigger bracket whose cost keeps increasing by 2 with each incoming (.

so why $$$+3$$$ ? $$$+3$$$ represents the increase in size of the bigger bracket by $$$+2$$$ and a creation of smaller bracket which costs $$$+1$$$ in total giving us a $$$+3$$$.

Here's an AC solution to the problem with this approach: 277181878

PS: This is my first blog so feel free to leave any suggestions/feedback and Sorry for any mistake i made :)

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

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

Auto comment: topic has been updated by BinaryBubble (previous revision, new revision, compare).

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

Auto comment: topic has been updated by BinaryBubble (previous revision, new revision, compare).

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

People were always saying how it was +1 or +3 but I had no clue why until you explained it. Thank you!

(I solved it in a different way, it took me a while to implement my messed up way)

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

am i the only one who write the solution like this:

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

    if you think about it adding $$$1$$$ for each _ is just doing $$$+1$$$ for both ( and ) and then you add $$$+2$$$ for each ( making it $$$+1$$$ for ) and $$$1 + 2 = +3$$$ for ( which is the solution i explained.

    You got some good pattern recognition skills lol.

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

I once heard a famous quote: "Any greedy solution can be solved using dp." — I forgot who.

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

    Exactly why i try to solve most of them with DP cuz my Greedy is very weak :(

    Congrats on expert btw Red0 <3

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

      Lol, I try to solve DP with greedy on the other hand cause my DP is weak :/

      And thanks for your kind words! I wish you best of luck in hitting expert in the near future as well! :D

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

    thats cuz its just the dp using greedy idea skull

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

Innovative approach! I'll have to do more dp problems to be more proficient in it.

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

Round No "168".

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

This reminds me while I'm in the contest, I tried dp as the first approach. But then when I write a few things off the notepad I found out it can be solved as greedy -> I drop dp and go greedy instantly.

The tale: my dp is weaker than greedy. Good to know someone else also approach the same way as I did.

Anyway, good luck in the next contest.

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

How to be that strong