题意:问n长度的序列,找出长度m的上升子序列的方案数。
分析:这个问题就是问:dp[i][j] = sum (dp[i-1][k]) (1 <= k <= n, a[k] < a[j]),当前长度i一层,最后下标j一层,之前的最后下标k一层,这样是n^3的复杂度。然后树状数组可以优化j,k的复杂度,就是j扫描一遍的同时,将a[j]的信息更新到树上,那么扫描就可以用 logn时间统计出k的信息,在这之前先对a[]离散化。
/************************************************ * Author :Running_Time * Created Time :2015/10/21 星期三 13:20:36 * File Name :C.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int N = 1e3 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const double EPS = 1e-8; struct BIT { int c[N], SZ; void add(int &a, int b) { a += b; while (a >= MOD) a -= MOD; } void init(int n) { memset (c, 0, sizeof (c)); SZ = n; } void updata(int i, int x) { while (i <= SZ) { add (c[i], x); i += i & (-i); } } int query(int i) { int ret = 0; while (i) { add (ret, c[i]); i -= i & (-i); } return ret; } }bit; int a[N], A[N]; int dp[N][N]; void compress(int n) { sort (A, A+n); int nn = unique (A, A+n) - A; for (int i=0; i<n; ++i) { a[i] = lower_bound (A, A+n, a[i]) - A + 1; } } int main(void) { int T, cas = 0; scanf ("%d", &T); while (T--) { int n, m; scanf ("%d%d", &n, &m); bit.init (n); for (int i=0; i<n; ++i) { scanf ("%d", &a[i]); A[i] = a[i]; } compress (n); memset (dp, 0, sizeof (dp)); for (int i=0; i<n; ++i) dp[1][i] = 1; int ans = 0; for (int i=2; i<=m; ++i) { bit.init (n); for (int j=0; j<n; ++j) { dp[i][j] = bit.query (a[j] - 1); bit.updata (a[j], dp[i-1][j]); } } for (int i=0; i<n; ++i) bit.add(ans, dp[m][i]); printf ("Case #%d: %d\n", ++cas, ans); } return 0; }
时间: 2024-10-26 21:58:50