少女祈祷中...

A - AtCoder Language

思路: 模拟即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
#define int long long

signed main() {
std::map<std::string, std::string> mp;
mp["red"] = "SSS";
mp["blue"] = "FFF";
mp["green"] = "MMM";
std::string s;
std::cin >> s;
if (mp.count(s)) {
std::cout << mp[s] << '\n';
} else {
std::cout << "Unknown\n";
}
return 0;
}

B - Get Min

思路:std::priority_queue<int, std::vector, std::greater>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
#define int long long

signed main() {
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
int q;
std::cin >> q;
while (q--) {
int op;
std::cin >> op;
if (op == 1) {
int x;
std::cin >> x;
pq.push(x);
} else {
std::cout << pq.top() << '\n';
pq.pop();
}
}
return 0;
}

C - King’s Summit

思路:考虑x方向上最大的距离和y方向上的最大距离, 取较大值上取整 ÷ 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
#define int long long

signed main() {
int n;
std::cin >> n;
int a = INT_MIN, b = INT_MAX, c = INT_MIN, d = INT_MAX;
for (int i = 0; i < n; i++) {
int x, y;
std::cin >> x >> y;
a = std::max(x, a);
b = std::min(b, x);
c = std::max(c, y);
d = std::min(d, y);
}
// std::cout << a << ' ' << b << '\n';
int ans = std::max((c - d + 1) / 2, (a - b + 1) / 2);
std::cout << ans << '\n';
return 0;
}

D - Substr Swap

思路 : 差分板子, 当前反转次数为奇数时输出 t对应字符, 否则相反

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
#define int long long

signed main() {
int n, q;
std::cin >> n >> q;
std::string s, t;
std::cin >> s >> t;
s = ' ' + s, t = ' ' + t;
std::vector<int> f(n + 2);
while (q--) {
int l, r;
std::cin >> l >> r;
f[l]++;
f[r + 1]--;
}
for (int i = 1; i <= n; i++) {
f[i] += f[i - 1];
}
for (int i = 1; i <= n; i++) {
if (f[i] & 1) {
std::cout << t[i];
} else {
std::cout << s[i];
}
}
std::cout << '\n';

return 0;
}

E - Subarray Sum Divisibility

思路: 注意到确定了第 i 个位置的数时, 第 i + L 个位置的数也是确定的,只需要考虑前L个数即可,预处理出 a[i] 变为 j 需要的操作次数 p[i] [j]。

定义 dfs(u, s) 表示到达 第 u 个位置时,总和 % m 为 s 的 状态, 当 u == L 时, 如果 s == 0 返回 0, 否则, 贪心的想,把不足的补到第L个数上是最优的 返回 (n / L)* (m - s)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>n
int p[510][510];
int memo[510][510];

signed main() {
int n, m, l;
std::cin >> n >> m >> l;
std::vector<int> a(n);
std::vector<std::vector<int>> b(l);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
b[i % l].push_back(a[i]);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < l; j++) {
int sum = 0;
for (auto x : b[j]) {
if (x <= i) {
sum += i - x;
} else {
sum += m + i - x;
}
}
p[j][i] = sum;
}
}
memset(memo, -1, sizeof(memo));
auto dfs = [&](auto&& dfs, int u, int s) -> int {
if (u == l) {
if (s == 0) {
return 0;
} else {
return (n / l) * (m - s);
}
}
int& res = memo[u][s];
if (res != -1) {
return res;
}
res = INT_MAX;
for (int i = 0; i < m; i++) {
res = std::min(res, dfs(dfs, u + 1, (s + i) % m) + p[u][i]);
}
return res;
};
int ans = dfs(dfs, 0, 0);
std::cout << ans << '\n';
return 0;
}

F、All Included

待补