sequence2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 124 Accepted Submission(s): 55
Problem Description
Given an integer array
bi
with a length of
n
, please tell me how many exactly different increasing subsequences.
P.S. A subsequence bai(1≤i≤k) is an increasing subsequence of sequence bi(1≤i≤n) if and only if 1≤a1<a2<...<ak≤n and ba1<ba2<...<bak .
Two sequences ai and bi is exactly different if and only if there exist at least one i and ai≠bi .
P.S. A subsequence bai(1≤i≤k) is an increasing subsequence of sequence bi(1≤i≤n) if and only if 1≤a1<a2<...<ak≤n and ba1<ba2<...<bak .
Two sequences ai and bi is exactly different if and only if there exist at least one i and ai≠bi .
Input
Several test cases(about
5
)
For each cases, first come 2 integers, n,k(1≤n≤100,1≤k≤n)
Then follows n integers ai(0≤ai≤109)
For each cases, first come 2 integers, n,k(1≤n≤100,1≤k≤n)
Then follows n integers ai(0≤ai≤109)
Output
For each cases, please output an integer in a line as the answer.
Sample Input
3 2 1 2 2 3 2 1 2 3
Sample Output
2 3
题意:给定n个元素组成的序列a[],问你长度为k的严格递增子序列的个数。
很水的dp,只不过需要高精度。。。模版出了点问题,手写了高精度。
思路:定义dp[i][j]为以a[i]结尾的长度为j胡严格递增子序列个数。
dp[i][j] += sigma(dp[p][j-1]) 其中(1 <= p < i && a[p] < a[i])。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define MAXN 100
#define MAXM 1010
#define eps 1e-8
#define LL long long
#define MAXSIZE 1005
using namespace std;
struct bign
{
int s[MAXSIZE];
void operator += (const bign &t)
{
for(int i = 0; i < MAXSIZE; i++)
{
s[i] += t.s[i];
if(s[i] >= 10)
{
s[i] -= 10;
s[i+1]++;
}
}
}
void put()
{
int i;
for(i = MAXSIZE - 1; i >= 0; i--)
if(s[i])
break;
if(i == -1)
printf("0");
else
for(; i >= 0; i--)
printf("%d", s[i]);
}
};
bign dp[101][101];
int a[101];
int main()
{
int n, k;
while(scanf("%d%d", &n, &k) != EOF)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= k; j++)
memset(dp[i][j].s, 0, sizeof(dp[i][j].s));
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]), dp[i][1].s[0] = 1;
for(int i = 1; i <= n; i++)
for(int j = 2; j <= min(i, k); j++)
for(int p = 1; p < i; p++)
if(a[p] < a[i])
dp[i][j] += dp[p][j-1];
bign ans; memset(ans.s, 0, sizeof(ans.s));
for(int i = 1; i <= n; i++)
ans += dp[i][k];
ans.put();
printf("\n");
}
return 0;
}