第四周训练赛总结
-a-b-c-
今天终于又轮到晓莱出题目了。
题目规则非常的简单,直接给定一个只由 ,, 构成的字符串 。
晓莱希望在 字符串上执行以下操作:
选择一个连续的 的子字符串,如果该字符串是
ABC将其替换为BCA。请你输出最多可以执行多少次操作。
暴力
暴力修改查找, 分。
#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 时 ,遇到其他情况 。
#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;
}数字变换
晓莱现在手上有一个数字 ,他希望你能通过一定的数字进行兑换得到数字。
其中你可以通过以下操作更改数字,但是需要支付一定数量的硬币:
- 将当前数乘,需要个硬币。
- 将当前数乘,需要个硬币。
- 将当前数乘,需要个硬币。
- 将当前数加或减,需要个硬币。
你可以按任意顺序和任意次数执行这些操作。
最少需要多少硬币才能得到?
为了增大题目的难度,设置了组测试用例。
暴力
dfs,应得 分,实得 分。
#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;
}正解
记忆化搜索,分类讨论具体见代码。
#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;
}红黑树
红黑树这里并非是你想象的红黑树。
晓莱在这里拿到了一棵只有红色和黑色两种节点颜色的树。
所谓树,即没有自环、重边和回路的无向连通图。
这棵树有 个顶点, 条边。每个顶点被染成了红色或者黑色。
晓莱想知道,取两个不同的点作为树上简单路径的起点和终点,它们的简单路径上有且仅有一个黑色点的取法有多少种?
补充描述:
- 树上两点之间的简单路径指连接两点的最短路。
- 和 的取法视为同一种。
暴力
分暴搜,乘法原理。
#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;
}正解
加上并查集优化,不重复跑联通块。
子集位运算
给一个长度为的数组 。要求维护两个操作:
修改数组中的一个位置。
询问区间所有子集位运算 之和,对 取模。
我们定义集合 , 为 子集。 所有子集位运算 之和为 。
暴力
枚举,期望 分。
正解
使用树状数组,维护区间的第 位的和,第 位的和为 ,其中 表示第 为前 个的和。
#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;
}