使用複雜度為o(nlogn)的演算法求解出最長上公升子串行以及子串行長度為n時最小的數值為多少
用陣列a儲存,舉例,a[2]=3
表示長度為2的子串行的最小數是3
因為通過使用二分法查詢數該插入的位置所以複雜度由o(n2)降為o(nlogn)
而後使用dfs(深搜)來求解具體的每乙個最長子序列,用ans陣列儲存,陣列開的較小,根據需要自行修改
!!!dfs需要注意不要重複搜尋,重複進入,會導致重複搜尋到然後重複輸出!!!注意邊界條件結果展示:
**部分:
#include
.h>
using namespace std;
int a[
100]
;int d[
100]
;int len;
int ans[
100]
;int search
(int x,int r)
else
if(a[mid]
>x)
else
}return l;
}void
dfs(int start,int flag)
printf
("\n");
return;}
for(int i=start;i>=
0;i--
)else}}
}}int main()
else
} cout<<
"最長上公升子串行長度:"
<
cout<<
"最長上公升子串行-表:"
<
for(int i=
1;i<=flag;i++
) cout<
len=flag;
cout<<
"具體最長上公升子串行:"
<
dfs(n-
1,flag)
;return0;
}
動態規劃 求最長上公升子串行長度
問題描述 求最長上公升子串行長度 include include include using namespace std const int maxn 1010 int a maxn 儲存輸入的資料 int maxlen maxn 儲存從開始到每乙個資料中最長上公升子串行長度 int main fo...
最長上公升子串行長度 LIS O nlogn 演算法
此前在動態規劃一講 動態規劃 3 最長遞增子串行 曾說過此問題,當前是的雙重迴圈是o n 2 的複雜度。後來在網上看到說lis問題有o nlogn 的演算法,於是拿來小研究了一下。這個演算法其實已經不是dp了,有點像貪心。至於複雜度降低其實是因為這個演算法裡面用到了二分搜尋。本來有n個數要處理是o ...
最長上公升子串行求長度
普通dp 複雜度o 2 1 include 2 using namespace std 34 const int n 1010 5 inta n dp n n 67 intmain 1819 int res 0 20 for int i 1 i n i 21 res max res,dp i 222...