跳到正文

题解

题解:AT_arc089_d [ARC089F] ColoringBalls

Felix72约 7 分钟
原文
文章信息与版本

文章信息

发布
2024年9月2日
收录
2026年10月28日
更新
2026年11月13日
篇幅
约 7 分钟
获赞
1

版本

  1. v1当前

计数 + dp 综合考验,可以看作好几道蓝题级别的东西综合运用出的黑。

求某个状态的最优策略

题目问的是总共能做出几种状态,我们先考虑一个朴素优美的暴力:枚举所有最终状态,再一一检验是否可以被得出。

这个做法显然没什么前途,但是给我们一个启发:不妨看看怎样以相对少的步数(或者说,相对简洁的过程)凑出某种特定状态。

事实上,这是很多这种技术的套路:它既能避免重复计数,又能引入更多限制,方便后续处理。

我们发现,对于一个由红蓝组成的极大连续段,它上面的第一次操作肯定是红色,第二次肯定是蓝色,后面呢?手玩一下发现,颜色不重要了,只要总操作次数 optsum⁡=B+1\operatorname{optsum} = B + 1 即可,其中 BB 是极大蓝色段的段数。

而对于一个纯红的连续段,一次操作可以直接得出。

组合计数 + dp

我们发现,如果我们知道到底有多少红蓝段(设为 xx),有多少纯红段(设为 yy),里面有多少极大蓝色段(设为 zz),把极大红、蓝、白色段全部缩成一个颜色不变的元素,能构成的排列数 fx,y,zf_{x,y,z},那么最终状态可以用组合数得出(式子暂时不重要,这里略去,可以看代码或者其他题解)。

如果我们预先枚举某些值,把另一些值设进 dp 状态并做动态规划,是不是就可以组合数 + dp 解决呢?事实是可以的。

xx 和 yy 显然不好设进状态,考虑外层枚举他们,并把 zz 设进状态,求 fzf_z。因为 xx 个连续段中每一个都需要起手一个 r 加一个 b,这个开始位置在操作序列中显然越靠左越好,贪心求位置即可;剩下的操作序列中,还需提取 yy 个最靠左的 r;剩下的众生平等,可用可不用,可以接在起手的 rb 后面增加极大蓝色段的段数。

统计方法

这里又有一个经典套路。

你发现可能会重复计算,因为 dp 出来某些极大非白色连续段本质相同,交换了也不会产生新状态。

因此你钦定 dp 时极大蓝色段的个数单调递增,对于本质相同的连续段一次枚举加若干个,注意,这里出现一只 log⁡\log(而不是 n2n^2),因为长度乘段数不大于总长度。

综上,你需要记录当前指针指向操作序列中的位置(等价于记录极大非白色连续段总数)、极大蓝色连续段总数、上一个极大非白色连续段中有几个极大蓝色连续段。状态三维,用前缀和优化转移,复杂度五次方乘一个对数,可以通过。

C++
#include <bits/stdc++.h>
using namespace std;

const int N = 75, mod = 1e9 + 7;
int n, m; string s;

long long c[355][355];
inline long long C(int n, int m)
{if(n < m) return 0; return c[n][m];}
inline void init()
{
	c[0][0] = 1;
	for(int i = 1; i <= 350; ++i)
	{
		c[i][0] = 1;
		for(int j = 1; j <= i; ++j)
			c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
	}
}

long long f[N][N][N], ans; int match[N], oc[N];
inline long long solve(int x, int y)
{
	memset(match, 0, sizeof(match));
	for(int i = 1, nx = 0, ny = 0; i <= m; ++i)
	{
		if(s[i] == 'r' && nx < x)
		{
			for(int j = i + 1; j <= m; ++j)
				if(s[j] == 'b' && !match[j])
					{match[i] = j, match[j] = i, ++nx; break;}
			if(!match[i]) return 0;
		}
		else if(s[i] == 'r' && ny < y) match[i] = i, ++ny;
		if(i == m && (nx < x || ny < y)) return 0;
	}
	
	memset(f, 0, sizeof(f));
	memset(oc, 0, sizeof(oc));
	f[0][0][0] = 1;
	for(int pos = m, i = 0, ps = m; pos >= 1; --pos)
	{
		if(match[pos] <= pos) continue;
		++i; oc[i] = oc[i - 1];
		while(ps > match[pos]) oc[i] += !match[ps--];
		for(int sig = 0; sig <= oc[i]; ++sig)
		{
			for(int nl = 1; nl <= oc[i]; ++nl)
			{
				f[i - 1][sig][nl] = (f[i - 1][sig][nl] + f[i - 1][sig][nl - 1]) % mod;
				for(int dlt = 1; dlt * nl <= sig && dlt <= i; ++dlt)
				{
					f[i][sig][nl] = (f[i][sig][nl] + f[i - dlt][sig - dlt * nl][nl - 1] * C(i, dlt) % mod) % mod;
					if(sig - dlt * nl > oc[i - dlt]) break;
				}
			}
		}
		f[i][0][0] = 1;
	}
	
	long long res = 0;
	for(int sig = 0; sig <= m; ++sig)
	{
		for(int nl = 0; nl <= m; ++nl)
		{
			if(!f[x][sig][nl]) continue;
			int all = n - (x + y - 1) - y - (2 * sig + x);
			int cnt = (2 * sig + x) + 2 * x + y + (x + y + 1);
//			if(C(all + cnt - 1, cnt - 1))
//			{
//				cerr << x << " " << y << " " << sig << " " << nl << " " << f[x][sig][nl] << '\n';
//				cerr << all << " " << cnt << " " << C(all + cnt - 1, cnt - 1) << '\n';
//			}
			res = (res + C(x + y, x) * C(all + cnt - 1, cnt - 1) % mod * f[x][sig][nl] % mod) % mod;
		}
	}
	return res;
}

int main()
{
//	freopen("text.in", "r", stdin);
//	freopen("prog.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	init();
	cin >> n >> m >> s;
	s = ' ' + s;
	for(int i = 0; i <= n; ++i)
		for(int j = 0; j <= n; ++j)
			ans = (ans + solve(i, j)) % mod;
	cout << ans << '\n';
	return 0;
}
/*
7 8
rbrrrrrb

4 3
rrb

10 7
rbrrbrr

5 5
rbrrb
*/
原文