http://acm.hdu.edu.cn/showproblem.php?pid=5542
Test instructions
- Given a sequence of length n, how many strict ascending sub-sequences of M are asked?
Ideas
- DP[I][J] Indicates how many strict ascending sub-sequences with a length of J ending with A[i]
- Dp[i][j]=sum{dp[k][j-1]},k less than I and A[k]<a[i]
- Interval DP, complexity O (n^3)
- Is timed out, and the third loop sum is accelerated with a tree-like array
- Time Complexity of O (N^2LOGN)
- Discretization of the time after sorting does not go heavy (otherwise WA)
"Accepted"
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>using namespaceStd;typedefLong Longll;Constll mod=1e9+7;Const intmaxn=1e3+3; ll A[maxn];ll B[maxn];ll DP[MAXN][MAXN];intCNT;intn,m;intLowbit (intx) { returnx& (-x);} ll Add (intXintY,ll D) { while(x<=N) {Dp[x][y]= (dp[x][y]+d)%MoD; X+=lowbit (x); }}ll sum (intXinty) {LL res=0LL; while(x) {res= (Res+dp[x][y])%MoD; X-=lowbit (x); } returnRes;}intMain () {intT; scanf ("%d",&u); intcas=0; while(t--) {memset (DP,0,sizeof(DP)); scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) {scanf ("%lld",&A[i]); } memcpy (B,a,sizeof(b)); Sort (a+1, a+n+1); //Cnt=unique (a+1,a+n+1)-a-1; for(intI=1; i<=n;i++) { intPos=lower_bound (A +1, a+n+1, B[i])-A; for(intj=1; J<=min (i+1, m); J + +) {ll tmp; if(j==1) tmp=1ll; ElseTmp=sum (pos-1, J-1); Add (pos,j,tmp); }} ll ans=sum (n,m); printf ("Case #%d:%d\n", ++cas, (int) ans); } return 0;}
View Code
Questions
Why can't we go heavy?
"Tree-like array +dp" HDU 5542 the Battle of Chibi