题意:

给出一个序列,删掉它的一个连续子序列(该子序列可以为空),使得剩下的序列有最长的连续严格递增子序列。

分析:

这个可以看作lrj的《训练指南》P62中讲到的LIS的O(nlogn)的优化变形过来的问题。

预处理:

Li是第i个元素Ai向左延伸的最大长度,即[i, i + Li - 1]是一个递增区间

同样地,Ri是第i个元素向右延伸的最大长度。

我们,可以枚举i, j(j<i 且 Aj < Ai),这样就可以把Aj和Ai“拼接”起来,所得到的最长连续递增子列的长度就是Lj + Ri

继续优化:

对于某一个i,如果有La = Lb 且 Aa < Ab,则后一个状态一定不会比前一个状态更优,因为如果Ab和Ai能“拼接”上,Aa和Ai也一定能“拼接”上,所以只要保留Aa即可

所以,用一个数组g,gi记录向左延伸为i的最小的元素值。

用lower_bound找到gk ≥ Ai 的第一个下标,(k-1则是gk-1 < Ai 的最后一个下标),此时得到的最大长度为k-1+Ri

然后还要更新g的值,g[L[i]] = min(g[L[i]], A[i])

UVa 1471 (LIS变形) Defense Lines_子序列UVa 1471 (LIS变形) Defense Lines_#include_02
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn = 200000 + 10;
 5 const int INF = 1000000000;
 6 
 7 int a[maxn], L[maxn], R[maxn], g[maxn];
 8 
 9 bool scan_d(int &ret)
10 {
11     char c;
12     if(c=getchar(),c==EOF) return 0; //EOF
13     while(c!='-'&&(c<'0'||c>'9')) c=getchar();
14     ret=(c=='-')?0:(c-'0');
15     while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
16     return 1;
17 }
18 
19 int main()
20 {
21     //freopen("in.txt", "r", stdin);
22 
23     int T;
24     scan_d(T);
25     while(T--)
26     {
27         int n;
28         scan_d(n);
29         for(int i = 0; i < n; ++i) scan_d(a[i]);
30         R[n-1] = 1;
31         for(int i = n-2; i >= 0; --i) R[i] = (a[i] < a[i+1]) ? (R[i+1] + 1) : 1;
32         L[0] = 1;
33         for(int i = 1; i < n; ++i) L[i] = (a[i] > a[i-1]) ? (L[i-1] + 1) : 1;
34 
35         int ans = 0;
36         for(int i = 1; i <= n; ++i) g[i] = INF;
37         for(int i = 0; i < n; ++i)
38         {
39             int k = lower_bound(g+1, g+1+n, a[i]) - g;
40             ans = max(ans, R[i] + k - 1);
41             if(a[i] < g[L[i]]) g[L[i]] = a[i];
42         }
43         printf("%d\n", ans);
44     }
45 
46     return 0;
47 }
代码君