ddp题解,就是 \(f[pos][o][l][r]\) 表示线段树上pos位置的区间是否选出最大值,以及左右端点有没有被去到时的最大值。然后用线段树维护依次取某个值为最小值的时候dp的最优解。
const int N = 2e5 + 5;
int T, n, a[N], f[N << 2][2][2][2];
inline int getmax(int pos) { return max(max(f[pos][1][0][0], f[pos][1][1][0]), max(f[pos][1][1][1], f[pos][1][0][1])); }
inline void update(int pos, int idx) {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) f[pos][i][j][k] = -1e9;
f[pos][0][0][0] = 0;
if (a[idx] < 0) return;
f[pos][0][1][1] = 1;
f[pos][1][1][1] = a[idx] + 1;
}
inline void pushup(int pos) {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) f[pos][i][j][k] = -1e9;
for (int a = 0; a < 2; a++)
for (int b = 0; a + b < 2; b++)
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int x = 0; x + j < 2; x++)
for (int y = 0; y < 2; y++)
chkmax(f[pos][a + b][i][y], f[pos << 1][a][i][j] + f[pos << 1 | 1][b][x][y]);
}
inline void build(int pos, int l, int r) {
if (l == r) { update(pos, l); return; }
int mid = l + r >> 1;
build(pos << 1, l, mid);
build(pos << 1 | 1, mid + 1, r);
pushup(pos);
}
inline void modify(int pos, int l, int r, int x) {
if (l == r) { update(pos, l); return; }
int mid = l + r >> 1;
if (x <= mid) modify(pos << 1, l, mid, x);
else modify(pos << 1 | 1, mid + 1, r, x);
pushup(pos);
}
vector <pii> vec;
signed main(void) {
for (read(T); T; T--) {
read(n); vec.clear();
for (int i = 1; i <= n; i++)
read(a[i]), vec.push_back(Mp(a[i], i));
build(1, 1, n); sort(vec.begin(), vec.end());
int ret = 0;
for (auto u : vec) {
int x = u.first, y = u.second;
chkmax(ret, x + getmax(1));
a[y] = -1;
modify(1, 1, n, y);
}
writeln(ret);
}
//fwrite(pf, 1, o1 - pf, stdout);
return 0;
}
标签:Min,int,Max,pos,Plus,vec,Size
From: https://www.cnblogs.com/EternalEpic/p/18445362