UP UP UP!
Time Limit: 1 Sec Memory Limit: 128 MB Submit: 92 Solved: 27SubmitStatusWeb Board
Description
题意很简单,给你长度为n的序列,找出有多少个不同的长度为m的严格上升子序列。(PS:相同子序列的定义为,每一个元素对应的下标都相同)
Input
输入数据第一行是个正整数T,表示总共有T组测试数据(T <= 5); 每组数据第一行为n和m,以空格隔开(1 <= n <= 100, 1 <= m <= n); 第二行为n个数,第i个数ai依次代表序列中的每个元素(1 <= ai <= 10^9);
Output
对于每组数据,输出一行Case #x: y,x表示当前测试数据的序号(从1开始),y表示结果。 需要注意的是,结果有可能很大,你需要将结果对1000000007(10^9+7)取余。
Sample Input
2 3 2 1 2 3 3 2 3 2 1
Sample Output
Case #1: 3 Case #2: 0
题解:
dp[i][j]代表前i个人j个递增子序列的长度;
if(a[i] > a[k])dp[i][j] = dp[i][j] + dp[k][j - 1](k < i);
代码:
1 #include<iostream> 2 #include<cmath> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN = 110; 8 int dp[MAXN][MAXN]; 9 int a[MAXN]; 10 const int MOD = 1e9 + 7; 11 int main() 12 { 13 int T, kase = 0, n, m; 14 scanf("%d", &T); 15 while (T--) { 16 scanf("%d%d", &n, &m); 17 for (int i = 1; i <= n; i++) { 18 scanf("%d", a + i); 19 } 20 memset(dp, 0, sizeof(dp)); 21 for (int i = 1; i <= n; i++) { 22 dp[i][1] = 1; 23 for (int j = 1; j <= i; j++) { 24 25 for (int k = 1; k < i; k++) { 26 27 if (a[k] < a[i]) { 28 29 dp[i][j] = (dp[i][j] + dp[k][j - 1]) % MOD; 30 31 // printf("%d %d ",i ,j); 32 } 33 } 34 } 35 } 36 int ans = 0; 37 for(int i = 1; i <= n; i++)ans = (ans + dp[i][m]) % MOD; //wocao 38 printf("Case #%d: %d ", ++kase, ans); 39 } 40 return 0; 41 } 42 43 /************************************************************** 44 Problem: 1868 45 User: handsomecui 46 Language: C++ 47 Result: Accepted 48 Time:18 ms 49 Memory:1376 kb 50 ****************************************************************/