LIS (DP)_代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 int max(int a, int b);
 5 int main()
 6 {
 7     int n;
 8     scanf("%d", &n);
 9     int i;
10     //arr stores data
11     int arr[101];
12     //path stores LIS result
13     int path[101];
14     //init
15     memset(arr, 0x00, sizeof(arr));
16     memset(path, 0x00, sizeof(path));
17     //input test data
18     for (i = 0; i < n; i++) {
19         scanf("%d", &arr[i]);
20     }
21 
22     for (i = 0; i < n; ++i) {
23         if(i==0){
24             path[i] = 1;
25         }
26         else {
27             //Before arr[i],path[0~i-1] has been found,
28             //if(arr[i-1] <= arr[i]) path[i] = path[i-1]+1;
29             if(arr[i-1] <= arr[i]){
30                 path[i] = path[i-1]+1;
31             }else {
32                 //or else compare arr[i] with arr[i-1~0] until arr[x] <= arr[i]
33                 int idx;
34                 for(idx=i-1;idx>=0;--idx){
35                     if(arr[idx]<=arr[i]){
36                         path[i] = path[idx]+1;
37                         break;
38                     }else {
39                         path[i] = 1;
40                         continue;
41                     }
42                 }
43 
44             }
45         }
46     }
47 
48     int max = 0;
49     for (i = 0; i < n; i++) {
50         if (max < path[i]) {
51             max = path[i];
52         }
53     }
54     printf("%d
", max);
55     return 0;
56 }
57 /*
58 6
59 5 3 4 8 6 7
60  */
61 int max(int a, int b)
62 {
63     return ((a >= b) ? a : b);
64 }
原文地址:https://www.cnblogs.com/guxuanqing/p/5656054.html