LIS(最長上公升子串行)

2021-08-21 10:08:56 字數 1049 閱讀 6591

在一段序列中,我們需要求出最長的上公升子串行的長度,我們可以使用動態規劃來進行統計

在遍歷資料是,不斷的與其前面的書進行比較,如果符合條件,就將長度加一併統計

動態規劃版

#include#include#include#include#includeusing namespace std;

const int maxn = 100005;

int num[maxn];

int dp[maxn];

int main()

int ans = 1;

for(int i = 2; i <= n; i++)

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

} printf("%d\n", ans);

} return 0;

}

也可以不斷的統計當前的數值能否大於臨時陣列最大值,加入臨時陣列末尾或者替換掉臨時陣列中第乙個大於它的數值,最後統計臨時陣列的長度

#include#include#includeusing namespace std;

const int maxn = 100005;

int num[maxn];

int dp[maxn];

int main()

else}}

} printf("%d\n", ans);

} return 0;

}

可是在資料大一點的時候會超時,我們可以用二分優化一下

#include#include#include#includeusing namespace std;

const int maxn = 100005;

int num[maxn];

int dp[maxn];

int main()

} printf("%d\n", ans);

} return 0;

}

這裡用到了lower_bound()函式,這個函式可以找到第乙個大於等於某數值的位址,非常好用

最長上公升子串行 LIS

題目 兩道題幾乎一樣,只不過對於輸入輸出的要求有所不同罷了。lis有兩種方法 一 第一種方法 時間複雜度為o n 2 狀態 dp i 區間為0 i的序列的lis 轉移方程 dp i max 1,dp k 1 0 k include include include include using name...

最長上公升子串行LIS

問題 給定n個整數a1,a2,a3,a4,a5,an,從左到右的順序盡量選出多個整數,組成乙個上公升子串行,相鄰元素不相等。例如 1,6,2,3,7,5,它的最長上公升子串行為 1,2,3,5。分析 剛開始想這個問題的時候我想用遞迴來解決問題,可是後來考慮到遞迴的時間複雜度高,就覺得不能使用,並且本...

LIS 最長上公升子串行

最長遞增子串行問題 在一列數中尋找一些數,這些數滿足 任意兩個數a i 和a j 若i 設dp i 表示以i為結尾的最長遞增子串行的長度,則狀態轉移方程為 dp i max,1 j 這樣簡單的複雜度為o n 2 其實還有更好的方法。考慮兩個數a x 和a y x 按dp t k來分類,只需保留dp ...