Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

dp: 参考了别人的,加了一些注释的版本,希望有助理解。

Posted by MonkAndMonkey at 2017-02-18 19:51:04 on Problem 2533
// 2533. 最长递增子序列
/*  // 误以为是dp[i-1]+1
    dp[i] = max(dp[j]+1, dp[i])
    dp 初始化为1!
*/
#include<iostream>
using namespace std;
#define MAXN 1002

int main(){
    int N,nbr;
    int n[MAXN],dp[MAXN];
    char ch;
    cin>>N;
    for(int i=0; i<=N; i++)
        dp[i] = 1;
    int m = 1;// 最大值
    for(int i=1; i<=N; i++){// i: 前i个的最长长度
        cin>>n[i];
        for (int j = 1; j<=i; j++){// j: 当前最大数的位置
            if (n[j] < n[i]){
                dp[i] = max(dp[j] + 1, dp[i]);
                if(dp[i] > m){
                    m = dp[i];
                }
            }
        }
    }
    cout<<m<<endl;
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator