跳到正文

题解

题解:P10729 [NOISG 2023 Qualification] Dolls

ThChamp约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年8月26日
收录
2026年9月23日
更新
2026年11月21日
篇幅
约 4 分钟
题目
P10729

版本

  1. v1当前

P10729 题解

一个无脑做法,考虑在值域上做这个问题,那么可以写出dp:

dpi=max⁡(dpi−1,dpi−2+visi)dp_i = \max(dp_{i-1},dp_{i-2}+vis_i)

visivis_i 指的是 ii 处是否存在值。

然后发现 V≤5×105V\le5\times 10^5,遂线段树维护矩阵类乘,非常无脑。

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define ls(x) ((x)<<1)
#define rs(x) (((x)<<1)|1)
#define DEBUG(x, y) (cerr << #x << ": " << (x) <<(y))

constexpr int MAXN = 3e5+10, MAXT = 2e6+100, MV = 5e5+10;
constexpr int INF = 1e9;
template<int N, int M, class T = int>
struct Mat{
	T a[N][M];
	Mat() {
		for (int i = 0; i<N; i++)
			for (int j = 0; j<M; j++)
				a[i][j] = -INF;
	}
	void init() {
		for (int i = 0; i<N; i++)
			for (int j = 0; j<M; j++)
				a[i][j] = -INF;
		for (int i = 0; i<N; i++)
			a[i][i] = 0;
	}
	T* operator[] (int i) {
		return a[i];
	}
	const T* operator[] (int i) const {
		return a[i];
	}
	
	void setv(int op) {
		a[0][0] = 0, a[0][1] = op, a[0][2] = -INF;
		a[1][0] = 0, a[1][1] = -INF, a[1][2] = -INF;
		a[2][0] = -INF, a[2][1] = 0, a[2][2] = -INF;
	}
	
//	void prt(string s) {
//		cerr << "prt " << s << " mat:" << endl;
//		for (int i = 0; i<N; i++) {
//			for (int j = 0; j<M;j ++)
//				cerr << a[i][j] << " \n"[j==M-1];
//		}
//	}
};

template<int N, int R, int M>
Mat<N, M> operator*(const Mat<N, R> &x, const Mat<R, M> &y) {
	Mat<N, M> res;
	for (int i = 0; i<N; i++)
		for (int j = 0; j<M; j++)
			for (int k = 0; k<R; k++)
				res[i][j] = max(res[i][j], x[i][k] + y[k][j]);
	return res;
}

int n;
Mat<3, 3> tmx[MAXT];

void pushup(int x) {
	tmx[x] = tmx[rs(x)] * tmx[ls(x)]; // 注意左乘 
}

void build(int x, int l, int r) {
	tmx[x].init();
	if (l == r) {
		tmx[x].setv(0);
		return;
	}
	int mid = (l+r)>>1;
	build(ls(x), l, mid);
	build(rs(x), mid+1, r);
	pushup(x);
}

void pupdate(int x, int l, int r, int q) {
	if (l == r) {
		tmx[x].setv(1);
		return;
	}
	int mid = (l+r)>>1;
	if (q <= mid) pupdate(ls(x), l, mid, q);
	else pupdate(rs(x), mid+1, r, q);
	pushup(x);
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	build(1, 1, MV);
	for (int i = 1; i<=n; i++) {
		int x;
		cin >> x;
		pupdate(1, 1, MV, x);
//		tmx[1].prt("tmx[1]");
		cout << max( { tmx[1][0][0], tmx[1][0][1], tmx[1][0][2] } ) << " \n"[i==n];
	}
	return 0;
}
原文