Problem link I have seen the solution but i dont understand how the recurrance relation is dp[i]=dp[i-1]+dp[i-2]+2;
Can somebody explain it to me.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
Problem link I have seen the solution but i dont understand how the recurrance relation is dp[i]=dp[i-1]+dp[i-2]+2;
Can somebody explain it to me.
Name |
---|
dp[i] consists of the following:
So total is dp[i-1] + dp[i-2] + 2
Do you think it is a good idea to write down small cases and try to find a pattern to deduce the recurrence relation?
Yes, definitely. I solved this problem mostly by looking at the cases for n = 3 and 4. Especially pay attention to the new subsequences that actually use the last character.
By the way, there's an alternative solution (actually the first one I thought of) that counts the sequences ending with R and B separately. Then, if the ith character is R, dp[i][R] = dp[i-1][R] + dp[i-1][B] + 1 (you can take any red-ending subsequence of n-1 and leave it alone or any blue-ending subsequence of n-1 and add the last character or take just the last character), and dp[i][B] = dp[i-1][B]. If the ith character is B, then dp[i][B] = dp[i-1][R] + dp[i-1][B] + 1, and dp[i][R] = dp[i-1][R].