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\n", 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值