First of all, to learn the longest common ascending subsequence LCIs, and then to optimize the algorithm for n*n efficiency, it is important to note that there can be a maximum in the middle of the sequence that can be obtained. Just beginning to reverse the input array, write down to find that there may be a problem. But the specifics are not carefully understood.
#include <bits/stdc++.h>using namespace STD;intdp[ -][ -],a[ -];intMain () {intN,_,i,j,k,ans,mx,t;scanf("%d", &_); while(_--) {scanf("%d", &n); for(i=1; i<=n; i++) {scanf("%d", &a[i]); }memset(DP,0,sizeof(DP)); ans=1; for(i=1; i<=n; i++) {mx=0; for(j=n; j>i; j--) {dp[i][j]=dp[i-1][J];if(a[i]>a[j]&&mx<dp[i-1][J]) mx=dp[i-1][J];if(a[i]==a[j]&&dp[i][j]<mx+1) dp[i][j]=mx+1;if(2*dp[i][j]>ans) ans=2*DP[I][J]; for(k=i; k<j; k++)if(A[k]>a[j]) {if(2*dp[i][j]+1>ans) ans=dp[i][j]*2+1; } } }printf("%d\n", ans); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4512 Series Stories-Perfect formation I (Longest public Ascent preface enhanced Edition)