最长不下降子序列(动态规划)
2011-12-14 16:04
351 查看
一个数的序列bi,当b1 <= b2 <= ... < =bS的时候,我们称这个序列是不下降的。对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些不下降的子序列(ai1, ai2, ..., aiK),这里1<= i1 < i2 < ... < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些不下降子序列,如(1, 7), (3, 4, 8)等等。这些子序列中最长的长度是4,比如子序列(1, 3, 5,
8)。
Input
多组cas , 每组cas 两行:
第一行 输入一个数 n (n < 10000), 表示有n个数
第二行 n个数, 分别代表每个数;
Output
每个cas 一行 输出 该书数列的最长的长度 ;
Sample Input
7
1 7 3 5 9 4 8
Sample Output
4
思路:从后面开始遍历
8)。
Input
多组cas , 每组cas 两行:
第一行 输入一个数 n (n < 10000), 表示有n个数
第二行 n个数, 分别代表每个数;
Output
每个cas 一行 输出 该书数列的最长的长度 ;
Sample Input
7
1 7 3 5 9 4 8
Sample Output
4
思路:从后面开始遍历
#include<iostream> #define MAX 10001 void vInput(int nArray[],int nN); int nGetResult(int nArray[],int nN); void vOutput(int nResult); int main(){ int nN; int nArray[MAX]; int nResult; while(1==scanf("%d",&nN)){ vInput(nArray,nN); nResult=nGetResult(nArray,nN); vOutput(nResult); } return 0; } void vInput(int nArray[],int nN){ int i; for(i=1;i<=nN;i++){ scanf("%d",&nArray[i]); } } int nGetResult(int nArray[],int nN){ int nF[MAX]; int nResult=1; int nTemp; int i; int j; for(i=1;i<=nN;i++){ nF[i]=1; } for(i=nN-1;i>=1;i--){ nTemp=1; for(j=i+1;j<=nN;j++){ if((nArray[i]<=nArray[j])&&nTemp<nF[j]+1){ nTemp=nF[j]+1; } } nF[i]=nTemp; if(nResult<nF[i]){ nResult=nF[i]; } } return nResult; } void vOutput(int nResult){ printf("%d\n",nResult); }
相关文章推荐
- 最长不下降子序列 动态规划
- (2)最长不下降子序列问题____动态规划
- 【动态规划】【二分】【最长不下降子序列】洛谷 P1020 导弹拦截
- 【C】动态规划--最大连续子序列和/最长不下降子序列/最长公共子序列/最长回文子串/DAG最长路
- hdu1160 FatMouse's Speed 最长下降子序列 动态规划
- 【动态规划】【二分】【最长下降子序列】XMU 1041 Sequence
- 求最长不下降序列(动态规划)
- 求最长不下降序列(动态规划)
- 动态规划实现最大连续子序列和,最长不下降子序列和最长公共子序列
- 最长不下降子序列 (动态规划)
- 【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence
- 【网络流24题】最长不下降子序列(最大流,动态规划)
- joj 2529 Chorus 动态规划 最长上升子序列和最长下降子序列
- 蓝桥杯算法训练拦截导弹【最长上升子序列 & 最长非下降子序列nlogn 和 n^2】
- 动态规划——求最长下降/上升子序列
- 动态规划——求最长下降/上升子序列
- POJ1088(二维最长下降序列?)(我觉得是搜索)
- 最长单调递增子序列--动态规划
- 动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )
- [dp] Testing the CATCHER 最长下降子序列问题