ZOJ 2136 Longest Ordered Subsequence

最长上升子序列,这个不解释,学动态规划第一个程序就是它吧;

#include<stdio.h> int main(void) { int n,geshu,i,j,nTmp,s=0; int a[1005]; int flag[1005]; scanf("%d",&n); while(n--) { scanf("%d",&geshu); for( i = 0 ; i < geshu ; i++ ) scanf("%d",&a[i]); flag[0] = 1; for( i = 1 ; i < geshu ; i++ )//以下是关键代码 { nTmp = 0; for( j = 0 ; j < i ; j++ ) if( flag[j] > nTmp && a[i] > a[j]) nTmp = flag[j]; flag[i] = nTmp + 1; } nTmp = -1; //判断最长的子序列 for( i = 0 ; i < geshu ; i++ ) if(flag[i] > nTmp ) nTmp = flag[i]; printf(s++?"/n%d/n":"%d/n",nTmp); } return 0; }

你可能感兴趣的:(ZOJ 2136 Longest Ordered Subsequence)