DP [I] [J] indicates an incremental subsequence whose length ends with I is J.
DP [I] [J] = sum (DP [k] [J]) k <I & A [I]> A [J]
If it is just a loop
For (j = 2; j <= m; ++ J)
For (I = 1; I <= N; ++ I)
For (k = 1; k <I; ++ K)
If (A [I]> A [J])
DP [I] [J] + = DP [k] [J-1];
The time complexity is O (n * m) TLE
However, K-cycle can be optimized with a tree array, K-cycle can be seen as a process of summation of the range, that is, to find the number of DP less than I [k] [J-1]
Then the time complexity becomes O (N * m * logn)
1 # include <algorithm> 2 using namespace STD; 3 typedef long ll; 4 const int n = 10000 + 10; 5 6 ll a [n], B [N], c [N]; 7 int N; 8 ll dp [N] [N]; 9 int lowbit (INT t) 10 {11 return T & (-t ); 12} 13 void Update (INT POs, ll Val) 14 {15 while (Pos <= N) 16 {17 C [POS] + = val; 18 POS + = lowbit (POS); 19} 20} 21 ll query (int pos) 22 {23 ll ans = 0; 24 while (Pos> = 1) 25 {26 ans + = C [POS] % 123456789; 27 pos-= lowbit (Pos); 28} 29 return ans; 30} 31 int main () 32 {33 int M, I, j; 34 while (scanf ("% d ", & N, & M )! = EOF) 35 {36 memset (DP, 0, sizeof (DP); 37 38 for (I = 1; I <= N; ++ I) 39 {40 DP [I] [1] = 1; 41 scanf ("% i64d", & A [I]); 42 B [I] = A [I]; 43} 44 sort (B + 1, B + n + 1); 45 for (j = 2; j <= m; ++ J) 46 {47 memset (C, 0, sizeof (c); 48 for (I = 1; I <= N; ++ I) 49 {50 int Index = lower_bound (B + 1, B + n + 1, a [I])-B; // discretization, index indicates the maximum number of 51 DP [I] in a Series [J] = query (index-1 ); // locate the number of 52 Update (index, DP [I] [J-1]) of incremental subsequences whose length is less than index; // update ended with I, number of 53} 54} 55 ll ans = 0; 56 for (I = 1; I <= N; ++ I) 57 ans = (ANS + dp [I] [m]) % 123456789; 58 printf ("% i64d \ n", ANS); 59} 60}
Bestcoder round # Eight 1003