Gym 100507G The Debut Album (滚动数组dp)
The Debut Album
题目链接:
http://acm.hust.edu.cn/vjudge/contest/126546#problem/G
Description
Pop-group “Pink elephant” entered on recording their debut album. In fact they have only two songs: “My love” and “I miss you”, but each of them has a large number of remixes. The producer of the group said that the album should consist of 𝑛 remixes. On second thoughts the musicians decided that the album will be of interest only if there are no more than 𝑎 remixes on “My love” in a row and no more than 𝑏 remixes on “I miss you” in a row. Otherwise, there is a risk that even the most devoted fans won’t listen to the disk up to the end. How many different variants to record the album of interest from 𝑛 remixes exist? A variant is a sequence of integers 1 and 2, where ones denote remixes on “My love” and twos denote remixes on “I miss you”. Two variants are considered different if for some 𝑖 in one variant at 𝑖-th place stands one and in another variant at the same place stands two.Input
The only line contains integers 𝑛, 𝑎, 𝑏 (1 ≤ 𝑎, 𝑏 ≤ 300; max(𝑎, 𝑏) + 1 ≤ 𝑛 ≤ 50 000).Output
Output the number of different record variants modulo 109 + 7.Examples
3 2 1 4Explanation
In the example there are the following record variants: 112, 121, 211, 212.##题意: 英语弱鸡表示题意真难看懂. 在n个位置填充1 or 2. 要求连续的1的个数不超过a,连续的2的个数不超过b. 求方案数.
##题解: 要考虑每一位可能放1或2,容易想到动态规划: dp[i][1/2][j]:到第i个位置时,放1且有连续j个1的方案(放2且有连续j个2的方案). 转移方程: j==1: dp[i][1][1] = Σdp[i-1][2][k] ,(k<=b), 即只放一个1的情况等同于前面有任意个连续2的情况. dp[i][2][1] = Σdp[i-1][1][k] ,(k<=a), 即只放一个2的情况等同于前面有任意个连续1的情况. j!=1: dp[i][1/2][j] = dp[i-1][1/2][j-1];
上述思路确定后,考虑数组内存的问题,由于n比较大,直接开三维数组估计会MLE. 由于转移具有线性型(i只能由i-1来). 所以可以用滚动数组压缩到二维数组. 注意考虑压缩后的各种问题: 如果后面需要的值在前面被改变了,那么就先需要记录下来. 对j进行枚举时,由于j用到j-1的值,所以要从大往小枚举.
一个简单的滚动数组dp写了一个小时,一直在瞎处理边界. 归根结底还是太弱了,dp还需多加练习.
##代码: ``` cpp #include

浙公网安备 33010602011771号