笔试算法学习--dp规划(最长非递增子序列)

来源:互联网 发布:win10 软件 卸载 残留 编辑:程序博客网 时间:2024/06/04 19:01

太懒,直接来个链接吧:

dp规划学习

个人心得:

1.自顶向下,从第一个数统计之后的最大子序列;

        2.自顶向下,问题重叠严重,冗余太多;采用自底向上;从最后一个数据开始处理,算法更高效;


#include <iostream>#include<vector>using namespace std;vector<int> num;int n;int D(){int max = 1;vector<int>d;for (int i = 0; i < n; i++)d.push_back(1);for (int i = n-2; i >=0; i--)for (int j = i + 1; j < n; j++)if ((num[i] >= num[j]) && (d[i] < (d[j] + 1))){d[i] = d[j] + 1;if (d[i]> max)max = d[i];}return max;}int main(){cin >> n;int tmp;for (int i = 0; i < n; i++){cin >> tmp;num.push_back(tmp);}cout << D()<< endl;system("pause");}




0 0