http://poj.org/problem?id=1836
求两遍最长上升子序列,顺序求一遍,逆序求一遍。
1 #include <stdio.h> 2 #include <string.h> 3 const int N=1002; 4 int dp1[N],dp2[N]; 5 double a[N]; 6 int main() 7 { 8 int n; 9 scanf("%d",&n); 10 for (int i = 1; i <= n; i++) 11 { 12 scanf("%lf",&a[i]); 13 } 14 dp1[1] = 1; 15 for (int i = 2; i <= n; i++) 16 { 17 int temp = 0; 18 for (int j = 1; j < i; j++) 19 { 20 if (a[i] > a[j]) 21 { 22 if (temp < dp1[j]) 23 temp = dp1[j]; 24 } 25 } 26 dp1[i] = temp+1; 27 } 28 dp2[n] = 1; 29 for (int i = n-1; i > 0; i--) 30 { 31 int temp = 0; 32 for (int j = n; j > i; j--) 33 { 34 if (a[i] > a[j]) 35 { 36 if (temp < dp2[j]) 37 temp = dp2[j]; 38 } 39 } 40 dp2[i] = temp+1; 41 } 42 int Max = 0; 43 for (int i = 1; i <= n; i++) 44 { 45 for (int j = i+1; j <= n; j++) 46 { 47 if (Max < dp1[i]+dp2[j]) 48 Max = dp1[i]+dp2[j]; 49 } 50 } 51 printf("%d ",n-Max); 52 return 0; 53 }