AC代码
1 #include <stdio.h> 2 const int MAXN = 200; 3 int dec[MAXN]; 4 int inc[MAXN]; 5 int n; 6 int input[MAXN]; 7 int main() 8 { 9 scanf("%d",&n); 10 for(int i = 1;i <= n;i++) 11 { 12 int tmp; 13 scanf("%d",&tmp); 14 input[i] = tmp; 15 } 16 for(int i = n;i >= 1;i--) 17 { 18 for(int j = i + 1;j <= n;j++) 19 { 20 if(input[i] > input[j]) 21 { 22 if(dec[j] + 1 > dec[i]) 23 dec[i] = dec[j] + 1; 24 } 25 } 26 } 27 for(int i = 1;i <= n;i++) 28 { 29 for(int j = i - 1;j >= 1;j--) 30 { 31 if(input[i] > input[j]) 32 { 33 if(inc[j] + 1 > inc[i]) 34 inc[i] = inc[j] + 1; 35 } 36 } 37 } 38 int tmp_max = 0; 39 for(int i = 1;i <= n;i++) 40 { 41 if(inc[i] + dec[i] > tmp_max) 42 tmp_max = inc[i] + dec[i]; 43 } 44 int ans = n - tmp_max - 1; 45 printf("%d",ans); 46 return 0; 47 }
思路:
左右两边分别计算最长上升和最长下降子序列长度
在所求出的最长上升和最长下降子序列长度中找到相加最大的就是所求者