[SCOI2014] 方伯伯的玉米田(dp+树状数组维护行列)

本文搬运自本人高中时期CSDN博客,若图片加载不出来,可到原文查看:https://blog.csdn.net/zhangtingxiqwq/article/details/142137107

https://www.luogu.com.cn/problem/P3287

显然每次操作的区间一定是一个后缀

我们直接令 dp(x,i)dp(x,i) 表示最后一个数是 xx (加之后),加了 ii 次的最长长度,转移显然 maxdp(yx,ji)\max dp(y\le x, j\le i)

映射到二维平面上,考虑每次相当于是更新一条斜线,每个点等于其左下角矩阵的最大值+1。那很好做,我们直接对行列都维护拿树状数组维护即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
#define M 510
//#define mo
#define N 5510
int n, m, i, j, k, T;
int ans, x, a[10010];

struct Bin1 {
int cnt[N];
int qry(int x) { ++x;
int ans = 0;
while(x) ans = max(ans, cnt[x]), x -= x & -x;
return ans;
}
void add(int x, int k) { ++x;
while(x < N) cnt[x] = max(cnt[x], k), x += x & -x;
}
}B1[M];

struct Bin2 {
int cnt[M];
int qry(int x) {
int ans = 0; ++x;
while(x) ans = max(ans, cnt[x]), x -= x & -x;
return ans;
}
void add(int x, int k) { ++x;
while(x < M) cnt[x] = max(cnt[x], k), x += x & -x;
}
}B2[N];

signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
n = read(); m = read();
for(i = 1; i <= n; ++i) a[i] = read();
for(i = 1; i <= n; ++i) {
x = 0;
for(k = 0; k <= m; ++k) {
x = max({x, B1[k].qry(a[i] + k), B2[a[i] + k].qry(k)});
B1[k].add(a[i] + k, x + 1); B2[a[i] + k].add(k, x + 1);
ans = max(ans, x + 1);
}
}
printf("%d", ans);
return 0;
}