http://acm.hdu.edu.cn/showproblem.php?pid=5542
题意:给出长度为N(1 <= N <= 1e3)的序列,求所有长度为M的递增子序列的个数。
解法:容易想到dp[i][j]以表示第i个元素结尾,lis为j的数量,状态转移方程:dp[i][k] = ∑dp[j][k-1] 1<=j< i, a[j] < a[i]. 时间复杂度为O(n3)会超时。
树状数组优化。构造dp[a[i]][j]以a[i]这个数结尾的lis长度为j的数量。因为a[i]数据范围1e9太大需要离散化。树状数组维护dp[a[i]][j] = dp[a[i]-1][j-1] + ..... dp[1][j-1]。1到a[i]-1 lis长度为j-1的数量
https://www.cnblogs.com/dilthey/p/9898230.html
//#include<bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <stack> #include <map> #include <set> #include <string.h> #include <vector> #include <stdlib.h> using namespace std; typedef long long ll ; #define int ll #define mod 1000000007 #define gcd(m,n) __gcd(m, n) #define rep(i , j , n) for(int i = j ; i <= n ; i++) #define red(i , n , j) for(int i = n ; i >= j ; i--) #define ME(x , y) memset(x , y , sizeof(x)) //int lcm(int a , int b){return a*b/gcd(a,b);} //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;} //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len} #define INF 0x3f3f3f3f #define PI acos(-1) #define pii pair<int,int> #define fi first #define se second #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() #define size(v) (int)(v.size()) #define cin(x) scanf("%lld" , &x); const int N = 1e6+9; const int maxn = 1e3+5; const double esp = 1e-6; int a[maxn] , b[maxn] , len; int dp[maxn][maxn]; int cnt ; int n , m ; int lowerbit(int x){ return x&(-x); } void add(int x , int y , int val){ while(x <= len){ dp[x][y] += val; dp[x][y] %= mod; x += lowerbit(x); } } int getsum(int x , int y){ int ans = 0 ; while(x){ ans += dp[x][y]; ans %= mod ; x -= lowerbit(x); } return ans ; } void solve(){ ME(dp , 0); scanf("%lld%lld" , &n , &m); rep(i , 1 , n){ scanf("%lld" , &a[i]); b[i] = a[i]; } sort(b+1 , b+1+n); len = unique(b+1 , b+1+n) - b - 1 ; rep(i , 1 , n){ rep(j , 1 , m){ int pos = lower_bound(b+1 , b+1+len , a[i]) - b ; if(j == 1){ add(pos , j , 1); }else{ add(pos , j , getsum(pos-1 , j-1)); } } } cout << "Case #" << ++cnt << ": " << getsum(len , m) << endl; } signed main() { //ios::sync_with_stdio(false); int t ; cin >> t ; while(t--){ solve(); } }