dp,开50棵线段树,每棵存到当前位置连续长为k的序列总数。注意代码中有一需要注意的地方,即当前数为最小数时,前一棵树的总和必然为0,不能通过求p - 1的总和数来求,此时p - 1 为-1会导致出错。另外,没离散化1e5超时,离散化1e4过了,spoj真是卡常数。。。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define N 10005
#define M 10000
#define INF 5000000
int n, k;
LL sum[55][N << 2];
int a[N], b[N];
void init(int id, int l, int r, int rt){
sum[id][rt] = 0;
if(l == r) return ;
int m = (l + r) >> 1;
init(id, lson);
init(id, rson);
}
void ins(int id, LL num, int v, int l, int r, int rt){
sum[id][rt] += num;
sum[id][rt] %= INF;
if(l == r) return ;
int m = (l + r) >> 1;
if(v <= m)ins(id, num, v, lson);
else ins(id, num, v, rson);
}
LL query(int id, int v, int l, int r, int rt){
if(l == r || v >= r)return sum[id][rt];
int ret = 0;
int m = (l + r) >> 1;
if(v <= m)ret += query(id, v, lson);
else ret += query(id, v, rson) + sum[id][rt << 1];
return ret % INF;
}
int main(){
while(scanf("%d%d", &n, &k) != EOF){
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + n);
int len = unique(b, b + n) - b;
for(int i = 0; i <= k; i++)
init(i, 0, M, 1);
for(int i = 0; i < n; i++){
int p = lower_bound(b, b + len, a[i]) - b;
ins(1, 1, p, 0, M, 1);
for(int j = 2; j <= k ;j++){
LL ret = 0;
if(p > 0)ret = query(j - 1, p - 1, 0, M, 1);
else ret = 0;//p = 0时直接赋0;
ins(j, ret, p, 0, M, 1);
}
}
printf("%lld\n", query(k, M, 0, M, 1));
}
return 0;
}