Gig Combinatorics (动态规划)

Your friend Tóti is an aspiring musician. He has written n songs, each of which has a hype rating of either 1, 2, or 3. A higher hype rating means the song has more energy. Tóti is planning his first live performance and needs your help. He wants to know how many setlists he can make. A setlist consist of at least three songs, the first song must have hype rating 1, the last song must have hype rating 3, and all other songs must have hype rating 2. Tóti also wants to play the songs in the same order he wrote them.
Given the hype rating of each of Tóti’s songs in the order he wrote them, how many setlists can he make?

Input
The first line of input consists of an integer n (1≤n≤106), the number of songs Tóti has written. The second line consists of n integers, each in {1,2,3}, giving the hype ratings of the n songs in the order they were written.

Output
Output the number of setlists Tóti can make. Since this number can be large, print it modulo 109+7.

Sample Input 1
9
1 1 1 2 2 2 3 3 3
Sample Output 1
63
Sample Input 2
8
1 2 1 2 3 1 2 3
Sample Output 2
15

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int N = 1e6+10;
int dp[4][N];
int a[N];
int n, i, j;
int main() 
{
    scanf("%d", &n);
    memset(dp, 0, sizeof(dp));
    for (i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (i = 1; i <= 3; i++) 
    {
        for (j = 1; j <= n; j++) 
        {
            if (a[j] == 1) 
            {
                if (i == 1)
                    dp[i][j] = (1 + dp[i][j - 1]) % M;
                else
                    dp[i][j] = (dp[i][j - 1]) % M;
            } 
            else if (a[j] == 2) 
            {
                if (i == 2)
                    dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1] * 2) % M;
                else
                    dp[i][j] = (dp[i][j - 1]) % M;
            } 
            else
            {
                if (i == 3)
                    dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1]) % M;
                else
                    dp[i][j] = (dp[i][j - 1]) % M;
            }
        }
    }
    printf("%d\n", dp[3][n]);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值