最長上升子序列 最長下降子序列
阿新 • • 發佈:2022-02-15
Longest Increasing subsequence
對於固定的陣列,雖然LIS序列不一定唯一,但LIS的長度是唯一的
2 7 1 5 6 4 3 8 9
尋找的話設定一個數組拷貝過來(和max_len同級別) 如果相同是求第一個,則設定為大於則交換 如果是相同求第二個,則設定為大於等圖則交換 (可能不能使用max函數了)
#include <bits/stdc++.h> using namespace std; const int maxn = 103, INF = 0x7f7f7f7f; int a[maxn], f[maxn]; int n,ans = -INF;int main() { scanf("%d", &n); for(int i=1; i<=n; i++) { scanf("%d", &a[i]); f[i] = 1; } for(int i=1; i<=n; i++) for(int j=1; j<i; j++) if(a[j] < a[i]) f[i] = max(f[i], f[j]+1);//狀態轉移方程 **** for(int i=1; i<=n; i++) ans = max(ans, f[i]); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int datas[1111]; int results[1111]; int main() { int n; int max_len = 0; cin>>n; for(int i=0;i<n;i++) { results[i]=1; cin>>datas[i]; } for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { if(datas[j]<datas[i])//前提,前邊的資料要小於當前的資料 { results[i]=max(results[j]+1,results[i]); } max_len=max(max_len,results[i]); } } cout<<max_len; return 0; }
本文來自部落格園,作者:firgk,部分內容轉載他人的部落格,時間原因沒有註明原文連結,侵權請聯絡我。
“隨筆”板塊僅僅用於個人記憶記錄和他人蔘考,質量較低,“文章”板塊用於記錄個人理解發現認識