POJ 1887 (DP)
题目:http://poj.org/problem?id=1887
题目本质:求最长递减子序列的长度。
状态转移方程:
d[i] = max(1,d[j]+1), 0<=j < i
代码:
1 #include <stdio.h> 2 #define N 5000 3 int missile[N]; 4 int ans[N]; 5 6 int main() 7 { 8 int first,n,i,j,max,c=1; 9 10 while( scanf("%d",&first) ) 11 { 12 if(-1 == first) break; 13 missile[0] = first; 14 15 i = 1; 16 while(scanf("%d",&first)) 17 { 18 if(-1 == first) break; 19 missile[i++] = first; 20 } 21 n = i; 22 23 max = 1; 24 ans[0] = 1; 25 for(i = 1 ; i < n ; ++i) 26 { 27 ans[i] = 1 ; 28 for(j = 0 ; j < i ; ++j) 29 if(missile[j] > missile[i] && ans[j] + 1 > ans[i] ) 30 ans[i] = ans[j] + 1; 31 32 if(ans[i] > max) max = ans[i]; 33 } 34 35 printf("Test #%d:\n",c++); 36 printf(" maximum possible interceptions: %d\n\n",max); 37 38 } 39 40 // system("pause"); 41 return 0; 42 }