動態規劃 最長不下降子串行 簡潔

2021-09-19 08:34:29 字數 679 閱讀 7193

問題描述:

求乙個數列的最長不下降子串行.

設有由n個不相同的整數組成的數列,記為:

a(1),a(2),...,a(n)且a(i)<>a(j) (i<>j)

若存在i1a(n-1),則存在長度為1的序列a(n-1)或a(n-2)

之後的過程類似,當前滿足條件找出後面序列長度的最大值進行加一.

#include#includeusing namespace std;

#define maxnumber 20

int src[maxnumber], b[maxnumber], index[maxnumber];

int main()

//進行動態規劃

for (int i = n - 2; i >= 0; --i)

if (max)

}} max = 0;

p = 0;

for (int i = 0; i < n; ++i) }

cout << "最長不下降序列長度為" << max << endl;

cout << src[p]<<" ";

p = index[p];

while (p)

cout << endl;

return 0;

}

動態規劃 最長不下降子串行

參考 每一次從頭掃瞄找出最佳答案。int a maxn d maxn intdp return ans 參考了這篇文章 那麼 如下 for int i 0 i n i scanf d a i memset dp,0x1f sizeof dp mx dp 0 for int i 0 i n i ans...

最長不下降子串行 動態規劃

一。問題描述 給定乙個序列a1 a2 a3 an現要求你從中找到最長的不下降子串行 二。問題分析 該問題可以和之前的max sum問題模擬,如果從決策的角度入手,直接引入並定義狀態f i 為 a i 所在子串行到i的長度 那我們的決策策略應該就是使得 f i 最大 所以基於這個分析我們很容易列出動態...

動態規劃 最長不下降子串行LIS

找出一組資料中最長的不下降子串行的長度。1,連續情況。即必須要求子數列連續相依 1 2 3 1 2 7 9中的最長子串行為3 1,2,3 易得遞推公式 if f i f i 1 dp i dp i 1 1 else dp i 1 includeusing namespace std define m...