跳到正文

题解

[CF1895G] Two Characters, Two Colors 题解

ToBeDetermined约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年10月27日
收录
2026年10月27日
更新
2026年11月8日
篇幅
约 10 分钟
题目
CF1895G

版本

  1. v1当前

完全不会流子咋办呢,我们会 DP。

看成默认不选获得 bib_i,如果选可以额外获得 wi=ri−biw_i = r_i - b_i,若 wi≤0w_i \le 0 则直接忽略。那么显然的 DP 是,若 si=1s_i = \texttt 1,有 dpi,j=max⁡(dpi−1,j,dpi−1,j−1+wi)dp_{i, j} = \max(dp_{i - 1, j}, dp_{i - 1, j - 1} + w_i);若 si=0s_i = \texttt 0,有 dpi,j=dpi,j+max⁡(wi−j,0)dp_{i, j} = dp_{i, j} + \max(w_i - j, 0)。

考虑怎么维护,容易想到从凸性出发。然而这个 DP 本身是没有凸性的,但注意到只有上凸壳上的点是有用的。一个简单的证明是,答案一定在凸壳上,且每种转移过后,不在凸壳上的点还是不在凸壳上。因此我们只用维护 DP 数组的上凸壳。

考虑按斜率从大到小维护凸壳上的线段。对于操作一,我们只需要插入线段 (1,wi)(1, w_i)。对于操作二,先额外维护一下第一条线段的截距即 dpi,0dp_{i, 0} 的值。把右端点横坐标 ≤wi\le w_i 的线段提出来,那么转移等价于让每条线段的斜率都减一。注意到此时会有一些线段不再在凸壳上,这些线段显然是一个后缀,因此先算出第一个横坐标 >wi\gt w_i 的点的位置,再根据这个点的位置删除一个后缀即可,形如建凸包的过程。

以上所有操作都可以用平衡树维护。写的 FHQ-Treap,具体可以参考代码。

Code
C++
#include <bits/stdc++.h>

#define rep(i, s, e) for(int i = s; i <= (e); ++i)
#define fep(i, s, e) for(int i = s; i < (e); ++i)
#define _rep(i, s, e) for(int i = s; i >= (e); --i)
#define _fep(i, s, e) for(int i = s; i > (e); --i)

#define int long long
#define pii pair<int, int>

using namespace std;

constexpr int inf = numeric_limits<int>::max() >> 1;
constexpr int ninf = numeric_limits<int>::min() >> 1;
constexpr int mod = 998244353;
constexpr double eps = 1e-9;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, a[400005], b[400005], ans;
string s;

struct Vector { int x, y; };
inline Vector operator+(const Vector &x, const Vector &y) { return {x.x + y.x, x.y + y.y}; }
inline bool operator<(const Vector &x, const Vector &y) { return __int128(1) * x.y * y.x < __int128(1) * y.y * x.x; }
inline bool operator>(const Vector &x, const Vector &y) { return __int128(1) * x.y * y.x > __int128(1) * y.y * x.x; }
inline bool operator<=(const Vector &x, const Vector &y) { return __int128(1) * x.y * y.x <= __int128(1) * y.y * x.x; }
inline bool operator==(const Vector &x, const Vector &y) { return __int128(1) * x.y * y.x == __int128(1) * y.y * x.x; }
inline bool operator>=(const Vector &x, const Vector &y) { return __int128(1) * x.y * y.x >= __int128(1) * y.y * x.x; }

struct Treap {
	struct Node {
		Vector val, sum;
		int sz, lz, key, ls, rs;
	} tr[400005];
	int rt, cnt;
	inline void update(int p, int d) {
		if(not p) return;
		tr[p].sum.y += d * tr[p].sum.x;
		tr[p].val.y += d * tr[p].val.x;
		tr[p].lz += d;
	}
	inline void pushdown(int p) {
		if(tr[p].lz) {
			update(tr[p].ls, tr[p].lz);
			update(tr[p].rs, tr[p].lz);
			tr[p].lz = 0;
		}
	}
	inline void pushup(int p) {
		tr[p].sum = tr[tr[p].ls].sum + tr[tr[p].rs].sum + tr[p].val;
		tr[p].sz = tr[tr[p].ls].sz + tr[tr[p].rs].sz + 1;
	}
	inline int create(const Vector &d) {
		++cnt, tr[cnt].val = tr[cnt].sum = d, tr[cnt].sz = 1;
		tr[cnt].lz = tr[cnt].ls = tr[cnt].rs = 0, tr[cnt].key = rnd();
		return cnt;
	}
	int merge(int p, int q) {
		if(not p or not q) return p | q; pushdown(p), pushdown(q);
		if(tr[p].key < tr[q].key) return tr[p].rs = merge(tr[p].rs, q), pushup(p), p;
		else return tr[q].ls = merge(p, tr[q].ls), pushup(q), q;
	}
	void split(int p, const Vector &d, int &x, int &y) {
		if(not p) return x = y = 0, void(); pushdown(p);
		if(tr[p].val > d) split(tr[p].rs, d, tr[x = p].rs, y);
		else split(tr[p].ls, d, x, tr[y = p].ls);
		pushup(p);
	}
	void split(int p, int d, int &x, int &y) {
		if(not p) return x = y = 0, void(); pushdown(p);
		int t = tr[tr[p].ls].sum.x + tr[p].val.x;
		if(d >= t) split(tr[p].rs, d - t, tr[x = p].rs, y);
		else split(tr[p].ls, d, x, tr[y = p].ls);
		pushup(p);
	}
	void split1(int p, const Vector &d, int &x, int &y) {
		if(not p) return x = y = 0, void(); pushdown(p);
		if(tr[p].val > tr[tr[p].rs].sum + d) split1(tr[p].rs, d, tr[x = p].rs, y);
		else split1(tr[p].ls, tr[tr[p].rs].sum + d, x, tr[y = p].ls);
		pushup(p);
	}
	inline Vector query(int p) {
		while(tr[p].ls) pushdown(p), p = tr[p].ls;
		return tr[p].val;
	}
	void update(int p, const Vector &d) {
		pushdown(p);
		if(tr[p].ls) update(tr[p].ls, d);
		else tr[p].val = d;
		pushup(p);
	}
	inline void insert(const Vector &d) {
		int x, y; split(rt, d, x, y);
		rt = merge(merge(x, create(d)), y);
	}
	inline void operate(int d) {
		if(tr[rt].sum.x <= d) return ans += d, update(rt, -1);
		int x, y, z; split(rt, d, x, z);
		Vector w = query(z);
		w.y -= d - tr[x].sum.x;
		ans += d, update(x, -1);
		split1(x, w, x, y);
		update(z, tr[y].sum + w);
		rt = merge(x, z);
	}
	int query(int p, int d) {
		if(not p) return 0;
		pushdown(p);
		int t = d + tr[tr[p].ls].sum.y + tr[p].val.y;
		return max(t, max(query(tr[p].ls, d), query(tr[p].rs, t)));
	}
} tr;

void solve() {
	cin >> n >> s, tr.rt = tr.cnt = ans = 0, tr.tr[0] = {};
	rep(i, 1, n) cin >> a[i];
	rep(i, 1, n) cin >> b[i], ans += b[i], a[i] -= b[i];
	rep(i, 1, n) if(a[i] > 0) {
		if(s[i - 1] == '1') tr.insert({1, a[i]});
		else tr.operate(a[i]);
	}
	cout << ans + tr.query(tr.rt, 0) << '\n';
	return;
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T = 1; cin >> T;
	while(T--) solve();
	return 0;
}
原文