最长不下降子序列

直接上代码:

#include<bits/stdc++.h>

using namespace std;

int a[1010], dp[1010];//存放数组

int main(){

     int n;//数组有几个
     cin >> n;//输入
     for(int i = 1;i <= n;i++){循环读入a数组
          cin >> a[i];
     }
     int ans = 0;//初始化
     for(int i = 1;i <= n;i++){//嵌套寻环用状态转移方程赋值dp数组    
          dp[i] = 1;
          for(int j = 1;j < i;j++){
               if(a[i] >= a[j] && (dp[j]+1 > dp[i])){
                      dp[i] = dp[j] + 1;
                }
          }
          ans = max(ans, dp[i]);
     }
     cout << ans;

     return 0;
}

posted @ 2022-02-02 20:52  吴杰锋  阅读(29)  评论(0编辑  收藏  举报