首页 > 代码库 > Super Jupmping ! Jumping ! Jumping

Super Jupmping ! Jumping ! Jumping

为什么写这一道呢,,,

因为这一道水啊,基础之基础之中的基础啊,,,,然而很简单啊,,,

简单的一道动态规划,最长上升子序列,,,LIS(Longest Increasing Subsequence)

dp[i]表示第i个数结尾的最长上升子序列的长度,,,//这里是数值

初始是dp[i]=1;seg[i]储存第i个数;

状态转移方程dp[i]=max(dp[i],dp[j]+1);

如果seg[j]<seg[i]并且j<i的话,就更新,取最大值;

# include <cstdio>
# include <iostream>
# include <algorithm>
# include <cstring>
using namespace std;

int n;
const int maxn=1e3+5;
int dp[maxn],seg[maxn];

int main(){
    while(scanf("%d",&n)!=EOF&&n){
        for(int i=0;i<n;i++)
            scanf("%d",&seg[i]);
        int ans=0;
        for(int i=0;i<n;i++){
            dp[i]=seg[i];
            for(int j=0;j<i;j++){
                if(dp[i]<dp[j]+seg[i]&&seg[j]<seg[i]){
                    dp[i]=max(dp[i],dp[j]+seg[i]);
                }
            }
            ans=max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

Super Jupmping ! Jumping ! Jumping