Longest Increasing Subsequence

Description

给出一组长度为nn的序列,a1,a2,a3,a4...an, 求出这个序列长度为k的严格递增子序列的个数

Input

第一行输入T组数据 T(0T10)
第二行输入序列大小n(1n100),长度k(1kn)
第三行输入n个数字ai(0ai1e9)

Output

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

Sample Input 1 

2
3 2
1 2 2
3 2
1 2 3

Sample Output 1

2
3
#include <bits/stdc++.h>
using namespace std;
int a[105], dp[105][105];
int n, k, ans = 0;
const int mod = 1e9+7;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        ans = 0;
        scanf("%d%d", &n, &k);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        memset(dp, 0, sizeof dp);
        for(int i = 1; i <= n; i++)
            dp[i][1] = 1;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                for(int k = 1; k < i; k++)
                    if(a[i] > a[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][k]) % mod;
        }
        printf("%d
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lesroad/p/9430876.html