日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Longest Increasing Subsequence(LIS入门dp)

發(fā)布時(shí)間:2024/9/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Longest Increasing Subsequence(LIS入门dp) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

http://poj.org/problem?id=2533
Longest Ordered Subsequence
Time Limit: 2000MS Memory Limit: 65536K

Description

A numeric sequence of ai is ordered if a1 < a2 < … < aN. Let the subsequence of the given numeric sequence (a1, a2, …, aN) be any sequence (ai1, ai2, …, aiK), where 1 <= i1 < i2 < … < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
Sample Input

7
1 7 3 5 9 4 8
Sample Output

4
/*

裸的LIS問(wèn)題: 對(duì)于LIS做dp,

下面是根據(jù)我的理解寫(xiě)的求解步驟,不足之處歡迎指出

1.確定最優(yōu)狀態(tài):設(shè)dp[i]為序列數(shù)組中i位置上的最優(yōu)狀態(tài)2. 考慮如何達(dá)到最優(yōu)狀態(tài):即dp[i]是怎么出來(lái)的,對(duì)于i位置的最優(yōu)狀態(tài)是由i之前的(設(shè)為j,0...j < i)最大dp[j]得來(lái)的,如果a[i] > a[j],那么dp[i] = max(dp[0...j])+1否則dp[i] = max(dp[0...j])3.由2推出狀態(tài)轉(zhuǎn)移方程:dp[i] = max(dp[j]+1,dp[i])(0..j,j<i&&a[i]>a[j])4.考慮邊界:在未dp前,對(duì)于每單獨(dú)的一個(gè)數(shù),dp[i] = 1;5.write code...

*/
AC_code:

#include <iostream> using namespace std; int dp[1005],a[1005];int main(){int n;cin>>n;for(int i = 0; i < n; i++){cin>>a[i];dp[i] = 1;}int ans = 0;for(int i = 0; i <n; i++){for(int j = 0; j < i; j++){if(a[i] > a[j]){dp[i] = max(dp[j]+1,dp[i]);}}ans = max(ans,dp[i]);}cout<<ans<<endl;return 0;}

總結(jié)

以上是生活随笔為你收集整理的Longest Increasing Subsequence(LIS入门dp)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。