CF2019 F. Max Plus Min Plus Size

EternalEpic發表於2024-10-03

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;
}

相關文章