最長不下降子串行 (LIS)

2021-09-26 05:59:45 字數 494 閱讀 5710

最長不下降子串行是這樣乙個問題:

在乙個數字序列中,找到乙個最長的子串行(可以不連續),使得這個子串行是不下降(非遞減)的。

令dp[i]表示以a[i]結尾的最長不下降子串行的長度,這樣對a[i]來說就會有兩種情況。

(1)如果存在a[i]之前的元素a[j](jdp[i]

(2)它前面的元素均比它大,則dp[i]=1;

狀態轉移方程為

dp[i]=max(1,dp[j]+1)

(j=1,2,……i-1&&a[j]<=a[i])

模板**如下:

#includeusing namespace std;

const int n=100;

int a[n],dp[n];

int main()

} ans=max(ans,dp[i]);

} printf("%d",ans);

return 0;

}

最長不下降子串行LIS

最長上公升子串行問題是解決很多問題的根本,它能幫助你理解二分的思想。考慮一下 對於乙個序列 n nn 請你查詢n nn中最長的子串行a aa,使得任意 i i j 時 a i a i a i a i a i a i 例如乙個長度為5 55的n nn 5553 331112 22444 顯然,它的最長...

LIS最長不下降子串行

在乙個序列中找到乙個最長的子串行 可以不連續 使得這個子串行不下降,即非遞減的。核心部分找到狀態轉移方程 dp i max j 1,2,i 1 a j 附上我的 include include includeusing namespace std const int maxn 10010 int d...

最長不下降子串行LIS

lis 題解 最長不下降子串行,英文縮寫為 lis longest increasing subsequence 其 定義是,設有由 n 個不相同的整數組成的數列,記為 a 1 a 2 a n 且 a i a j i j 例如 3,18,7,14,10,12,23,41,16,24。若存在 i1h ...