Transfer from http://www.cppblog.com/mysileng/archive/2012/11/30/195841.html
Maximum increment subsequence problem: Look for some number in a column number, these number satisfies: Any two number a[i] and a[j], if i<j, must have a[i]<a[j], so the longest subsequence is called the longest increment subsequence.
Set Dp[i] Indicates the length of the longest increment subsequence ending with I, then the state transition equation is:
Dp[i] = max{dp[j]+1}, 1<=j<i,a[j]<a[i].
This simple complexity is O (n^2), in fact there is a better way.
Consider two numbers a[x] and a[y],x<y and A[x]<a[y], and Dp[x]=dp[y], when a[t] to choose, which is the best to take the composition? Obviously choose a[x] more potential, because there may be a[x]<a[z]<a[y], so a[t] can get better values. Give us a revelation here, when Dp[t] is the same, try to choose a smaller a[x].
Sort by dp[t]=k, just keep the minimum value in all A[t] of dp[t]=k, set D[k] record this value, d[k]=min{a[t],dp[t]=k}.
At this point, note the two characteristics of D (important):
1. D[k] In the calculation process does not rise in monotony;
2. d array is orderly, d[1]<d[2]< D[n].
With these two properties, it is convenient to solve:
1. Set the length of the longest ascending subsequence that is currently calculated to Len (initial 1) and read a new element x each time:
2. If X>d[len] is added directly to the end of D and len++; (Use property 2)
Otherwise, in d the binary lookup, find the first number smaller than x D[k], and d[k+1]=x, here x<=d[k+1] must be established (nature).
/** 2. Maximum increment sub-sequence O (NLOGN) algorithm: 3. State transition equation: f[i] = max{f[i],f[j]+1},1<=j<i,a[j]<a[i]. 4. Analysis: Adding x<y,f[x]>=f[y], X has more potential relative to Y. 5. First classify according to f[] value, record the minimum value that satisfies f[t]=k a[t], remember d[k]=min{a[t]},f[t]=k. 6. 1. Found that d[k] does not increase in monotony by 7 during the calculation. 2.d[1]<d[2]<...<d[k] (disprove) 1 2 3 8 4 7 8. Solution: 9.1. Set the current longest increment subsequence to Len, considering element A[i]; 10.2. If d[len]<a[i], then len++, and will d[len]=a[i]; 11. Otherwise, in D[0-len], find the first element smaller than it d[k], and D[k+1]=a[i]. ().*/ -. #include <iostream> -. #include <cstdio> the. #include <cstring> -.using namespacestd; -.Const intN =41000; -.intA[n];//A[i] Raw data +.intD[n];//D[i] The minimum value of the increment subsequence of length I -. +.intBinsearch (intKeyint* D,intLowintHigh ) A. { at. while(low<=High ) -. { -.intMid = (Low+high) >>1; -.if(Key>d[mid] && key<=d[mid+1]) -.returnmid; -.Else if(key>D[mid]) in. Low = mid+1; -.Else to. High = mid-1; +. } -.return 0; the.} *. $.intLIS (intAintNint*d)Panax Notoginseng. { -.inti,j; the. d[1] = a[1]; +.intLen =1;//Increment sub-sequence length A. for(i =2; I <= N; i++) the. { +.if(d[len]<A[i]) -. j = + +Len; $.Else $. j = Binsearch (A[i],d,1, Len) +1; -. D[J] =A[i]; -. } the.returnLen; -.} Wuyi. the.intMain () -. { Wu.intT; -.intp; About. scanf"%d",&t); $. while(t--) -. { -. scanf"%d",&p); -. for(inti =1; I <= p; i++) A. scanf"%d",&A[i]); +. printf"%d\n", LIS (a,p,d)); the. } -.return 0; $.}
Longest ascending subsequence O (Nlogn) solution (RPM)