跳到正文

算法·理论

第四周训练赛

wbw_121124约 12 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 12 分钟
获赞
1

版本

  1. v1当前

第四周训练赛总结

题目分数改题用时
-a-b-c-50(TLE)??? min???\ min
数字变换35(WA+TLE)??? min???\ min
红黑树100(AC)−1 min-1\ min
子集位运算0(WA,有模糊思路)≤10 min\le 10\ min

-a-b-c-

今天终于又轮到晓莱出题目了。

题目规则非常的简单,直接给定一个只由 AA,BB,CC 构成的字符串 SS。

晓莱希望在 SS 字符串上执行以下操作:

选择一个连续的 SS 的子字符串,如果该字符串是 ABC 将其替换为 BCA。

请你输出最多可以执行多少次操作。

暴力

暴力修改查找,5050 分。

C++
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
int n, cnt;
string s;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> s;
	n = s.size();
	s = '#' + s;
	for (int i = 1; i <= n - 2; i++)
		if (s[i] == 'A' && s[i+1] == 'B' && s[i+2] == 'C')
		{
			cnt++;
			s[i] = 'B';
			s[i + 1] = 'C';
			s[i + 2] = 'A';
			i -= 2;
			if(i < 0)
				i++;
		}
	cout << cnt;
	return 0;
}

正解

我们发现可以统计 A 的个数,遇到 BC 时 ans := ans + A的个数ans\ :=\ ans\ +\ \text{A的个数},遇到其他情况 cnt := 0cnt\ :=\ 0。

C++
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
int n, cnt, ans;
string s;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> s;
	n = s.size();
	s = '#' + s + '#';
	for(int i = 1; i <= n; i++)
		if (s[i] == 'A')
			cnt++;
		else if (s[i] == 'B' && s[i + 1] == 'C')
			ans += cnt, i++;
		else
			cnt = 0;
	cout << ans;
	return 0;
}

数字变换

晓莱现在手上有一个数字00 ,他希望你能通过一定的数字进行兑换得到数字NN。

其中你可以通过以下操作更改数字,但是需要支付一定数量的硬币:

  • 将当前数乘22,需要AA个硬币。
  • 将当前数乘33,需要BB个硬币。
  • 将当前数乘55,需要CC个硬币。
  • 将当前数加11或减11,需要DD个硬币。

你可以按任意顺序和任意次数执行这些操作。

最少需要多少硬币才能得到NN?

为了增大题目的难度,设置了TT组测试用例。

暴力

dfs,应得 5050 分,实得 3535 分。

C++
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
int t,n,a,b,c,d;
int dfs(int x,int sum)
{
	if(sum>=log2(n))
		return LLONG_MAX/4;
	if(x==0)
		return 0;
	int mini=LLONG_MAX;
	if(!(x%2))
		mini=min(mini,dfs(x/2,sum)+a);
	if(!(x%3))
		mini=min(mini,dfs(x/3,sum)+b);
	if(!(x%5))
		mini=min(mini,dfs(x/5,sum)+c);
	return min({mini,dfs(x-1,sum+1)+d,dfs(x+1,sum+1)+d});
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin>>t;
	while(t--)
	{
		cin>>n>>a>>b>>c>>d;
		cout<<dfs(n,0)<<'\n';
	}
	return 0;
}

正解

记忆化搜索,分类讨论具体见代码。

C++
#include<bits/stdc++.h>
typedef int int32;
#define int unsigned long long
using namespace std;
int t,n,a,b,c,d;
map<int,int>dp;
int dfs(int x)
{
	if(x==0)
		return 0;
	if(dp[x])
		return dp[x];
	dp[x]=x*d;
	if(x%2)
		dp[x]=min({dp[x],dfs(x/2)+x%2*d+a,dfs((x+2-x%2)/2)+(2-x%2)*d+a});
	else
		dp[x]=min(dp[x],dfs(x/2)+a);
	if(x%3)
		dp[x]=min({dp[x],dfs(x/3)+x%3*d+b,dfs((x+3-x%3)/3)+(3-x%3)*d+b});
	else
		dp[x]=min(dp[x],dfs(x/3)+b);
	if(x%5)
		dp[x]=min({dp[x],dfs(x/5)+x%5*d+c,dfs((x+5-x%5)/5)+(5-x%5)*d+c});
	else
		dp[x]=min(dp[x],dfs(x/5)+c);
	return dp[x];
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin>>t;
	while(t--)
	{
		cin>>n>>a>>b>>c>>d;
		dp.clear();
		cout<<dfs(n)<<'\n';
	}
	return 0;
}

红黑树

红黑树这里并非是你想象的红黑树。

晓莱在这里拿到了一棵只有红色和黑色两种节点颜色的树。

所谓树,即没有自环、重边和回路的无向连通图。

这棵树有 nn 个顶点, n−1n−1 条边。每个顶点被染成了红色或者黑色。

晓莱想知道,取两个不同的点作为树上简单路径的起点和终点,它们的简单路径上有且仅有一个黑色点的取法有多少种?

补充描述:

  1. 树上两点之间的简单路径指连接两点的最短路。
  2. (u,v)(u, v)和 (v,u)(v, u)的取法视为同一种。

暴力

100100 分暴搜,乘法原理。

C++
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n,dp[N],dp1[N],ans,sum;
vector<int>nbr[N];
string s;
int dfs(int x,int fa)
{
	int sum=1;
	for(auto& nxt:nbr[x])
		if(nxt!=fa&&s[nxt]=='R')
			sum+=dfs(nxt,x);
	return sum;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin>>n>>s;
	s='#'+s;
	for(int i=1;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		nbr[x].push_back(y);
		nbr[y].push_back(x);
	}
	for(int i=1;i<=n;i++)
		if(s[i]=='B')
		{
			sum=1;
			for(auto& nxt:nbr[i])
				if(s[nxt]=='R')
				{
					int tmp=dfs(nxt,i);
					ans+=sum*tmp;
					sum+=tmp;
				}
		}
	cout<<ans;
	return 0;
}

正解

加上并查集优化,不重复跑联通块。

子集位运算

给一个长度为nn的数组 a1,a2,...,ana_1,a_2,...,a_n。要求维护两个操作:

  1. 修改数组中的一个位置。

  2. 询问区间[l,r][l,r]所有子集位运算 andand 之和,对 109+710^9+7 取模。

我们定义集合 S={l,l+1,...,r}S=\{l,l+1,...,r\},TT 为 SS 子集。 fT=aT1 and aT2 and...and aTk(k为集合T的大小,若k=0则fT=0)f_T=a_{T_1}\ and\ a_{T_2}\ and...and\ a_{T_k}(k为集合T的大小,若k=0则f_T=0) 所有子集位运算 andand 之和为 ∑T∈SfT\sum_{\\T\in S}f_T。

暴力

O(2r−l+1)O(2^{r-l+1}) 枚举,期望 00 分。

正解

使用树状数组,维护区间的第 xx 位的和,第 ii 位的和为 (2query(i,r)−query(i,l−1)−1)×2i\left(2^{\text{query}(i,r)-\text{query}(i,l-1)}-1\right)\times 2^i,其中 query(i,j)\text{query}(i,j) 表示第 ii 为前 jj 个的和。

C++
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m, a[N], tree[32][N], dp[N], dp1[N], mod = 1e9 + 7;
int lowbit(int x)
{
	return x & -x;
}
void update(int mi, int x, int k)
{
	while (x <= n)
	{
		tree[mi][x] += k;
		x += lowbit(x);
	}
	return;
}
int query(int mi, int x)
{
	int sum = 0;
	while (x)
	{
		sum += tree[mi][x];
		x -= lowbit(x);
	}
	return sum;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		dp[i] = (dp[i - 1] * 2) % mod;
		if (!dp[i])
			dp[i] = 1;
		for (int j = 0; j < 32; j++)
			if ((a[i] >> j) & 1)
				update(j, i, 1);
	}
	dp[0] = mod / 2 + 1;
	cin >> m;
	for (int i = 1; i <= m; i++)
	{
		int opt, x, y;
		cin >> opt >> x >> y;
		if (opt == 1)
		{
			for (int j = 0; j < 32; j++)
				if ((a[x] >> j) & 1)
					update(j, x, -1);
			a[x] = y;
			for (int j = 0; j < 32; j++)
				if ((a[x] >> j) & 1)
					update(j, x, 1);
		}
		else
		{
			int ans = 0;
			for (int j = 0; j < 32; j++)
			{
				int tmp = query(j, y) - query(j, x - 1);
				ans = (ans + (dp[tmp] * 2 % mod + mod - 1) % mod * ((1ll << j) % mod)) % mod;
			}
			cout << ans << '\n';
		}
	}
	return 0;
}
原文