The "BZOJ4361" Isndescription gives a sequence of length n (a1,a2 ... AN). If the sequence A is not non-descending, you must delete the number from the operation until a is not reduced. Ask how many different ways of operation, answer modulo 10^9+7. Input first line an integer n. The next row of n integers describes a.
Output
An integer line that describes the answer.
Sample Input4
1 7 5 3Sample Output -hint1<=n<=2000
Solving the puzzle: think of the dynamic + tree array + tolerance, but the coefficient of tolerance to think complex ~
We want to first find out the number of all non-descending sequences of length I, which can be solved by DP. Set F[i][j] means the maximum value is I, the length of J non-descending sequence number, with a tree-like array to optimize the transfer.
Then use G[i] to represent $\sum f[...] [j]$. Because the other number of deleted order can be randomly selected, so g[i]*= (n-i)!. But it is possible to cut in half and have got a non-descending sequence, how to remove the illegal state? Let's scold!
First think of a half-day allowance coefficient, but in fact g[i]-=g[i+1]* (i+1) can. It is just g[i+1] to get a non-descending sequence in the general context.
Time Complexity $o (n\log N) $
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;typedef Long Long ll;const int p=1000000007;int n,m;ll ans;int val[2010],v[2010],p[2010];int s[2010][2010];ll f[2010 ],c[2010][2010],jc[2010],jcc[2010],ine[2010];inline Int Rd () {int Ret=0,f=1;char gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+ (gc^ ' 0 '), Gc=getchar (); return ret*f;} BOOL CMP (const int &A,CONST int &b) {return val[a]<val[b];} inline void updata (int x,int y,int z) {if (!z) return; f[y]+=z;for (int i=x;i<=m;i+=i&-i) {s[y][i]+=z;if (S[y][i] >=p) s[y][i]-=p;}} inline int query (int x,int y) {int i,ret=0;for (i=x;i;i-=i&-i) {ret+=s[y][i];if (ret>=p) ret-=p;} return ret;} int main () {n=rd (); int i,j;jc[0]=jc[1]=jcc[0]=jcc[1]=ine[0]=ine[1]=1;for (i=2;i<=n;i++) jc[i]=jc[i-1]*i%p,ine[i] =p-(p/i) *ine[p%i]%p,jcc[i]=jcc[i-1]*ine[i]%p;for (i=0;i<=n;i++) {c[i][0]=1;for (j=1;j<=i;j++) c[i][j]= (C[I-1][J-1]+C[I-1][J])%P;} for (i=1;i<=n;i++) val[i]=rd (), P[i]=i;sort (P+1,P+N+1,CMP), and for (i=1;i<=n;i++) {if (i==1| | VAL[P[I]]>VAL[P[I-1]]) m++;v[p[i]]=m;} for (i=1;i<=n;i++) {for (j=i;j>=2;j--) Updata (V[i],j,query (v[i],j-1)); Updata (v[i],1,1);} for (i=1;i<=n;i++) f[i]=f[i]%p*jc[n-i]%p;for (i=1;i<=n;i++) f[i]= (f[i]-f[i+1]* (i+1)%P+P)%P,ans= (ans+f[i])%P; printf ("%lld", ans); return 0;} 4 1 2) 3 4
"BZOJ4361" isn dynamic planning + tree-like array + tolerance