AtCoder Beginner Contest 380
A - 123233#
题意#
给个
位数,判断是否是 个 , 个 , 个 。
思路#
模拟。
代码#
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 1e6 + 5;
void solve()
{
string s;
cin >> s;
int a = 0, b = 0, c = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '1')
{
a++;
}
else if (s[i] == '2')
{
b++;
}
else if (s[i] == '3')
{
c++;
}
}
if (a == 1 && b == 2 && c == 3)
{
cout << "Yes" << endl;
return;
}
cout << "No" << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
B - Hurdle Parsing header#
题意#
给个字符串,输出被"|"隔开的"-"每块有几个。
思路#
模拟。
代码#
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 1e6 + 5;
void solve()
{
string s;
cin >> s;
int ans = 0;
for (int i = 1; i < s.length(); i++)
{
if (s[i] == '|')
{
cout << ans << " ";
ans = 0;
}
else
{
ans++;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
C - Move Segment#
题意#
给个
串,可以分为 -块和 -块(连续的 / )。现在将第 个 -块移动到第 个 -块之后,输出移动后的字符串。(题目保证至少有 个 -块)
思路#
代码#
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 1e6 + 5;
int st[mxn], ed[mxn];
void solve()
{
int n, k;
cin >> n >> k;
string s;
cin >> s;
int cnt = 1, t = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '1')
{
if (!t)
{
st[cnt] = i;
}
t++;
}
else
{
if (t)
{
ed[cnt] = i - 1;
cnt++;
t = 0;
}
}
}
if (t)
{
ed[cnt] = s.size() - 1;
}
string ans = s;
for (int j = ed[k - 1] + (ed[k] - st[k]) + 2; j <= ed[k]; j++)
{
ans[j] = '0';
}
for (int i = ed[k - 1] + 1; i <= ed[k - 1] + (ed[k] - st[k]) + 1; i++)
{
ans[i] = '1';
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
D - Strange Mirroring#
题意#
给定字符串
,进行如下操作10^{100}次:颠倒 中的大小写得到 ,令 。后有 次询问,输出每次询问 中第 个位置的字符。
思路#
容易发现每次字符串的出都会
,那就看要查询的位置是在当前字符串的前半段还是后半段,前者不需要颠倒大小写,后者需要,同时后半段对应前半段的位置为 。我们可以每次都让当前查询位置 落在后半段,每次都翻转一下。(开始写的循环有点问题,后面换成递归解决了)
代码#
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mxn = 1e6 + 5;
string s;
inline char change(char c)
{
return isupper(c) ? tolower(c) : toupper(c);
}
char f(int k, bool flag)
{
int len = s.size();
if (k < len)
{
return flag ? change(s[k]) : s[k];
}
while (k >= (len << 1))
{
len <<= 1;
}
return f(k - len, !flag);
}
void solve()
{
int q;
cin >> s >> q;
while (q--)
{
int k;
cin >> k;
k--;
putchar(f(k, false));
putchar(' ');
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
E - 1D Bucket Tool#
题意#
给定
面墙,初始每面墙的颜色各不相同。进行 次操作,每次操作有两种形式:
:将第 和与其相邻的颜色相同的墙染成 这种颜色。
:输出颜色为 的墙的数量。
思路#
用并查集。记录每种颜色的根、大小(数量多少)、左右端点。详情见代码。
代码#
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mxn = 5e5 + 5;
int color[mxn], pre[mxn], siz[mxn], L[mxn], R[mxn], col_size[mxn];
int find(int x)
{
return x == pre[x] ? x : pre[x] = find(pre[x]);
}
void join(int a, int b)
{
int fa = find(a);
int fb = find(b);
if (fa != fb)
{
// 更新区间
L[fa] = min(L[fa], L[fb]);
R[fa] = max(R[fa], R[fb]);
siz[fa] += siz[fb];
pre[fb] = fa;
}
}
void paint(int x, int c)
{
int fx = find(x);
col_size[color[fx]] -= siz[fx]; // 原色块变小
color[fx] = c; // 染色
col_size[c] += siz[fx]; // 新色块变大
// 染色完检查两边颜色是否相同,相同则合并
int l = L[fx], r = R[fx];
if (color[find(l - 1)] == c)
{
join(l - 1, x);
}
if (color[find(r + 1)] == c)
{
join(r + 1, x);
}
}
void solve()
{
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
pre[i] = color[i] = L[i] = R[i] = i;
siz[i] = col_size[i] = 1;
}
while (q--)
{
int cmd, x, c;
cin >> cmd;
if (cmd == 1)
{
cin >> x >> c;
paint(x, c);
}
else
{
cin >> c;
cout << col_size[c] << endl;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T = 1;
//cin >> T;
while (T--)
{
solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效