D. Longest ascending sub-sequence
S. Attention is strictly incremented
C.O (NLOGN)
#include <iostream>#include<stdio.h>using namespacestd;Const intmaxn=1005;intA[MAXN],B[MAXN];//B[k] is the smallest end element value in sequence A of all increments of K//find a location using a binary search method to make num>b[i-1] and num<b[i] and use num instead of b[i]intSearch (intNumintLowintHigh ) { intmid; while(low<=High ) {Mid= (Low+high)/2; if(Num>=b[mid]) low=mid+1; Elsehigh=mid-1; } returnLow ;}intDP (intN) { intI,len,pos; b[1]=a[1]; Len=1; for(i=2; i<=n;i++){ if(A[i]>b[len]) {//if A[i] is larger than the maximum size of the b[] array, insert it directly into the backlen=len+1; B[len]=A[i]; } Else{//in the b[] array, find the first position larger than a[i] and let A[i] replace the position .Pos=search (A[i],1, Len); B[pos]=A[i]; } } returnLen;}intMain () {intN; inti; while(~SCANF ("%d",&N)) { for(i=1; i<=n;++i) {scanf ("%d",&A[i]); } printf ("%d\n", DP (N)); } return 0;}View Code
C2. O (n^2)
#include <iostream>#include<stdio.h>using namespacestd;#defineMAXN 1005intA[MAXN];intDP[MAXN];intMain () {intN; inti,j; inttemp; intans; while(~SCANF ("%d",&N)) { for(i=1; i<=n;++i) {scanf ("%d",&A[i]); } dp[1]=1; for(i=2; i<=n;++i) {Temp=0; for(j=1; j<i;++j) { if(a[i]>A[j]) { if(temp<Dp[j]) {Temp=Dp[j]; }}} Dp[i]=temp+1; } ans=0; for(i=1; i<=n;++i) { if(dp[i]>ans) {ans=Dp[i]; }} printf ("%d\n", ans); } return 0;}View Code
POJ-2533 longest Ordered subsequence (longest ascending subsequence)