AtCoder Beginner Contest 158
比赛链接:https://atcoder.jp/contests/abc158/tasks
A - Station and Bus
题意
给出一个由 'A','B' 组成的长为 3 的字符串,判断串中是否有 'A','B' 相邻。
代码
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; cout << (s.find('A') != -1 and s.find('B') != -1 ? "Yes" : "No"); }
B - Count Balls
题意
先放置 $a$ 个蓝色球,然后放置 $b$ 个红色球,如此循环,问前 $n$ 个球中有多少个蓝色球。
代码
#include <bits/stdc++.h> using namespace std; int main() { long long n, a, b; cin >> n >> a >> b; cout << a * (n / (a + b)) + min(a, n % (a + b)); }
C - Tax Increase
题意
找出满足 $\lfloor \frac{8n}{100} \rfloor = a$ 和 $\lfloor \frac{10n}{100} \rfloor = b$ 的最小的 $n$ 值。
代码
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; for (int i = 1; i <= int(1e6); i++) { if (i * 8 / 100 == a and i * 10 / 100 == b) { cout << i; return 0; } } cout << -1; }
D - String Formation
题意
开始时有一个字符串 $s$,接下来有 $q$ 个操作:
- 当 $t = 1$ 时,反转字符串
- 当 $t = 2$ 时:
- 当 $f = 1$ 时,向字符串首部添加字符 $c$
- 当 $f = 2$ 时,向字符串尾部添加字符 $c$
题解
利用双端队列模拟即可,考虑到反复反转字符串时间成本太高,而且反转后影响的只是添加字符的位置,可以用一个标记表示每次应向首或尾部添加字符。(或者记录 $t = 1$ 出现的次数)
代码
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; deque<char> que(s.begin(), s.end()); int q; cin >> q; bool rev = false; for (int i = 0; i < q; i++) { int t; cin >> t; if (t == 1) { rev = !rev; } else { int f; char c; cin >> f >> c; if (f == 1) { if (rev) { que.push_back(c); } else { que.push_front(c); } } else { if (rev) { que.push_front(c); } else { que.push_back(c); } } } } if (rev) reverse(que.begin(), que.end()); for (char c : que) cout << c; }
E - Divisible Substring
题意
求一个至多长为 200000 的包含 0 ~ 9 的字符串中有多少连续子串转为 10 进制后为素数 $p$ 的倍数。(前导 0 的情况也算)
题解
和 ABC164D 类似,但需要特别讨论 $p = 2$ 和 $p = 5$ 的情况,因为此时以 $p$ 或 $p$ 的倍数结尾的数都是 $p$ 的倍数。
代码
#include <bits/stdc++.h> using namespace std; int main() { int n, p; cin >> n >> p; string s; cin >> s; long long ans = 0; if (p == 2 or p == 5) { for (int i = 0; i < n; i++) if ((s[i] - '0') % p == 0) ans += i + 1; } else { map<int, int> mp; mp[0] = 1; int sum = 0, power = 1; for (int i = n - 1; i >= 0; i--) { sum = (sum + (s[i] - '0') * power) % p; ans += mp[sum % p]++; power = power * 10 % p; } } cout << ans; }
F - Removing Robots
题意
数轴上有 $n$ 个机器人,每次可以激活一个未激活的机器人,机器人会移动 $d_i$ 个距离并激活沿途的其他机器人,问有多少个未激活的机器人的集合。
题解
好像需要用到线段树或栈,待填。

浙公网安备 33010602011771号