【线段树优化 dp】做法,相对思维难度较低。
我们注意到猫只会从高的点跳到矮的点,因此设计 表示跳到 的最大价值。如果 可以跳到 ,令 则 ,则若 ,需要满足:
否则若 ,则:
令 可以转移时 ,否则 。
则有转移方程:
随后我们考虑分类讨论 的情况与 的情况:
我们注意到 可以转移到的 即 是可以表示的,我们用单调栈维护在 左边第一个大于其的位置 和右边第一个大于其的元素 。则我们可以考虑主动转移,将 转移到 ,不难发现转移方程中:
- 若 ,则 中, 需要最大化。
- 若 ,则 中, 需要最大化。
由此维护 与 ,使用线段树维护动态规划即可。
时间复杂度 。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int a[1000005], f1[1000005], f2[1000005], p[1000005];
int t[4000005], lazy[4000005];
int t2[4000005], lazy2[4000005];
int dp[1000005];
int n;
void push_down(int p) {
if (lazy[p] != -1e18) {
t[p * 2] = max(t[p * 2], lazy[p]);
t[p * 2 + 1] = max(t[p * 2 + 1], lazy[p]);
lazy[p * 2] = max(lazy[p * 2], lazy[p]);
lazy[p * 2 + 1] = max(lazy[p * 2 + 1], lazy[p]);
lazy[p] = -1e18;
}
}
void push_down2(int p) {
if (lazy2[p] != -1e18) {
t2[p * 2] = max(t2[p * 2], lazy2[p]);
t2[p * 2 + 1] = max(t2[p * 2 + 1], lazy2[p]);
lazy2[p * 2] = max(lazy2[p * 2], lazy2[p]);
lazy2[p * 2 + 1] = max(lazy2[p * 2 + 1], lazy2[p]);
lazy2[p] = -1e18;
}
}
void build(int p, int l, int r) {
t[p] = -1e18;
lazy[p] = -1e18;
t2[p] = -1e18;
lazy2[p] = -1e18;
if (l == r) return;
int mid = (l + r) / 2;
build(p * 2, l, mid);
build(p * 2 + 1, mid + 1, r);
}
void update(int p, int l, int r, int ql, int qr, int val) {
if (ql <= l && r <= qr) {
t[p] = max(t[p], val);
lazy[p] = max(lazy[p], val);
return;
}
push_down(p);
int mid = (l + r) / 2;
if (ql <= mid) update(p * 2, l, mid, ql, qr, val);
if (qr > mid) update(p * 2 + 1, mid + 1, r, ql, qr, val);
t[p] = max(t[p * 2], t[p * 2 + 1]);
}
void update2(int p, int l, int r, int ql, int qr, int val) {
if (ql <= l && r <= qr) {
t2[p] = max(t2[p], val);
lazy2[p] = max(lazy2[p], val);
return;
}
push_down2(p);
int mid = (l + r) / 2;
if (ql <= mid) update2(p * 2, l, mid, ql, qr, val);
if (qr > mid) update2(p * 2 + 1, mid + 1, r, ql, qr, val);
t2[p] = max(t2[p * 2], t2[p * 2 + 1]);
}
int query(int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return t[p];
push_down(p);
int mid = (l + r) / 2;
int res = -1e18;
if (ql <= mid) res = max(res, query(p * 2, l, mid, ql, qr));
if (qr > mid) res = max(res, query(p * 2 + 1, mid + 1, r, ql, qr));
return res;
}
int query2(int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return t2[p];
push_down2(p);
int mid = (l + r) / 2;
int res = -1e18;
if (ql <= mid) res = max(res, query2(p * 2, l, mid, ql, qr));
if (qr > mid) res = max(res, query2(p * 2 + 1, mid + 1, r, ql, qr));
return res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
p[a[i]] = i;
dp[i] = -1e18;
}
build(1, 1, n);
stack<int> s;
for (int i = n; i >= 1; i--) {
while (!s.empty() && a[s.top()] <= a[i]) s.pop();
f1[i] = (s.empty() ? n + 1 : s.top());
s.push(i);
}
while (!s.empty()) s.pop();
for (int i = 1; i <= n; i++) {
while (!s.empty() && a[s.top()] <= a[i]) s.pop();
f2[i] = (s.empty() ? 0 : s.top());
s.push(i);
}
int ma = 0;
for (int i = n; i >= 1; i--) {
int pos = p[i];
if (i == n) {
dp[i] = 0;
} else {
if (1 <= pos - 1) {
int l = query(1, 1, n, pos, pos);
if (l != -1e18) {
dp[i] = max(dp[i], l + pos);
}
}
if (pos + 1 <= n) {
int r = query2(1, 1, n, pos, pos);
if (r != -1e18) {
dp[i] = max(dp[i], r - pos);
}
}
}
if (dp[i] != -1e18) {
if (pos + 1 <= f1[pos] - 1) {
update(1, 1, n, pos + 1, f1[pos] - 1, dp[i] - pos);
}
if (f2[pos] + 1 <= pos - 1) {
update2(1, 1, n, f2[pos] + 1, pos - 1, dp[i] + pos);
}
}
ma = max(ma, dp[i]);
}
cout << ma;
return 0;
}