Longest Increasing Subsequence (第三次积分赛)

描述
给出一组长度为n的序列, a 1 a 2 a 3 . . . . . . a n 求出这个序列长度为kk的严格递增子序列的个数

输入
第一行输入T组数据 T (0≤T≤10)
第二行输入序列大小n(1≤n≤100),长度k(1≤k≤n)
第三行输入n个数字

输出
数据规模很大, 答案请对1e9+7取模

输入样例 1
2
3 2
1 2 2
3 2
1 2 3

输出样例 1
2
3
思路:dp
LIS的 d p i 是以i结尾的最长的子序列的程度
此题 d p i , j 是以i结尾j为长度的子序列的数量
AC code

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 110;
const int mod = (int)1e9+7;

int arr[maxn],dp[maxn][maxn];

int main(){
    int t; cin>>t;
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        int n,m; scanf("%d %d",&n,&m);
        for (int i = 1;i<=n;i++) {
            scanf("%d",&arr[i]);
            dp[i][1] = 1;
        }
        for (int i = 1;i<=n;i++) {
            for (int j = 2;j<=i;j++) {
                for (int k = 1;k<i;k++) {
                    if ( arr[i] > arr[k] ) {
                        dp[i][j] = (dp[i][j] + dp[k][j-1]) % mod;
                    }
                }
            }
        }
        int ans = 0;
        for (int i = 1;i<=n;i++) {
            ans = ( ans + dp[i][m] ) % mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Acer12138/article/details/81456519