LIS 问题 二分查找优化

按n=5,a-{4,2,3,1,5}为例

dp的值依次是:

INF INF INF INF INF

4     INF INF INF INF

2     INF INF INF INF

2     3     INF INF INF 

1     3     INF INF INF 

1     3     5     INF INF

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<map>
 6 #include<vector>
 7 #include<set>
 8 #include<string>
 9 #include<cmath>
10 #include<cstring>
11 using namespace std;
12 int a[1010];
13 int n; 
14 int dp[1010];//长度为i+1的上升子序列中末尾元素的最小值(不存在是INF) 
15 int INF=0x3f3f3f3f;
16 void solve()
17 {
18     fill(dp,dp+n,INF); 
19     for(int i=0;i<n;i++)
20     {
21         *lower_bound(dp,dp+n,a[i])=a[i];
22     }
23 //    for(int i=0;i<n;i++)
24 //        printf("%d ",dp[i]);
25 //        printf("\n");
26     printf("%d\n",lower_bound(dp,dp+n,INF)-dp)z-=;
27 }
28 
29 int main()
30 {
31     scanf("%d",&n);
32     for(int i=0;i<n;i++)
33         scanf("%d",&a[i]);
34     solve();
35     return 0;
36 }

猜你喜欢

转载自www.cnblogs.com/fudanxi/p/12296611.html