2025-7-21专题训练总结
2025-7-21代码源专题训练总结
开始直接看 J,看了
非常的简单,
判断是否
x \in [x_0, x_1], y \in [y_0, y_1], z \in [z_0, z_1] 。#include <bits/stdc++.h> using namespace std; int z0, h, u0, v0, u1, v1, q, lx, ly, rx, ry; int main() { cin >> z0 >> h >> u0 >> v0 >> u1 >> v1 >> q; lx = min(u0, u1), ly = min(v0, v1); rx = max(u0, u1), ry = max(v0, v1); for (int x, y, z; q; q--) { cin >> x >> y >> z; cout << (lx <= x && x <= rx && ly <= y && y <= ry && z0 <= z && z <= z0 + h ? "YES" : "NO") << "\n"; } return 0; }
然后做 B 题去了。写了
思路:
字符串最多左移
7 次,所以暴力枚举一下即可。#include <bits/stdc++.h> using namespace std; int t, n, k; string s; int C(string s) { if (n < 7) return 0; int c = 0; for (int i = 1; i + 6 <= n; i++) c += s.substr(i, 7) == "nanjing"; int r = c; for (int i = 1; i <= min(n, k); i++) { i + 6 <= n && (c -= s.substr(i, 7) == "nanjing"); c += s[i] == 'g' && (s.substr(n + i - 6) == "nanjin"); s += s[i], r = max(r, c); } return r; } int main() { for (cin >> t; t; t--) { cin >> n >> k >> s, s = " " + s; cout << C(s) << "\n"; } return 0; }
然后是 C 题:
思路:
由于数据范围很小,可以直接暴力枚举,用
map统计即可。#include <bits/stdc++.h> using namespace std; int n, m, k; map<string, vector<pair<int, int>>> f; int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { string s = ""; for (int l = 1; l <= k; l++) { char c; cin >> c, s += c; } f[s].push_back({i, j}); } for (auto [s, v] : f) v.size() == 1 && (cout << v[0].first << " " << v[0].second, exit(0), 1); return 0; }
看了下榜单,发现很多人都过了 E,然后来看了 E:
RE 了一发,原因:set 在删除元素是忘记判断是否存在,f.find(s) != f.end()。
思路:
将文本转小写,存入
set,会的单词直接删除即可,答案就是set的元素个数。#include <bits/stdc++.h> using namespace std; string s, t; int n; unordered_set<string> f; int main() { getline(cin, s), s += ' '; for (int i = 0; i < s.size(); i++) { if (s[i] == ' ') f.insert(t), t = ""; else s[i] != ',' && s[i] != '.' && s[i] != '!' && s[i] != '?' && (t += s[i] >= 'A' && s[i] <= 'Z' ? s[i] + 32 : s[i], 1); } for (cin >> n; n; n--) cin >> s, f.find(s) != f.end() && (f.erase(s)); cout << f.size(); return 0; }
接着卡在了 D,一直没什么思路,也没找到什么规律,直到下午才想到做法。
思路:
可以证明,要先将所有石像统一方向,最后进行
2 操作。枚举
i ,则同一方向需要s_i = a_{(i + 1) \bmod 4} + 2a_{(i + 2) \bmod 4} + 3a_{(i + 3) \bmod 4} ,其中a_i 表示初始时朝向i 方向的石像个数,最后朝向为(s_i + i) \bmod 4 。最终答案为
\min\limits_{0 \le i \le 3} s_i + (4 - (s_i + i) \bmod 4) \bmod 4 。#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n, s[4]; int main() { cin >> n; for (int i = 1, x; i <= n; i++) cin >> x, s[x]++; int r = 1e9; for (int k = 0; k < 4; k++) { int c = 0; for (int i = 1; i < 4; i++) c += i * s[(k + i) % 4]; r = min(r, c + ((4 - (c + k) % 4) % 4)); } cout << r; return 0; }
F 题:由于忘记考虑时针的偏转,吃了
思路:
暴力枚举目标时间,计算最小代价即可。
#include <bits/stdc++.h> using namespace std; int t, x, y, lx, ly, rx, ry; int C(int x, int y, int r) { return min(abs(x - y), r - abs(x - y)); } int main() { for (cin >> t; t; t--) { cin >> x >> y >> lx >> ly >> rx >> ry; int r = 1e9; pair<int, int> z; for (int i = rx; i >= lx; i--) for (int j = (i == rx ? ry : 59); j >= (i == lx ? ly : 0); j--) { int s = C(60 * x + y, 60 * i + j, 720) + C(12 * y, 12 * j, 720); s <= r && (r = s, z = {i, j}, 1); } cout << z.first << " " << z.second << "\n"; } return 0; }
然后是一道简单的模拟题 H,错因:读错题,痛 WA 两发。
思路:
按题意模拟即可。
#include <bits/stdc++.h> using namespace std; using LL = long long; using Pii = pair<int, LL>; const int N = 1e5 + 5; int t, n, m, q; LL s[N]; Pii c[N]; unordered_map<int, bool> a[N]; int main() { cin.tie(0)->sync_with_stdio(0); for (cin >> t; t; t--) { cin >> n >> m >> q; fill(s + 1, s + m + 1, 0); for (int i = 1; i <= m; i++) a[i].clear(); int f = 0, l; for (int o, p, x; q; q--) { cin >> o >> p; if (o == 1) f = p, l = m; if (o == 2) { cin >> x; x == f && !a[p][x] && (s[p] += l--, a[p][x] = 1); } if (o == 3) cin >> x, x == f && (a[p][x] = 1); } for (int i = 1; i <= m; i++) c[i] = {i, s[i]}; sort(c + 1, c + m + 1, [](Pii x, Pii y) { return x.second != y.second ? x.second > y.second : x.first < y.first; }); for (int i = 1; i <= m; i++) cout << c[i].first << " " << c[i].second << "\n"; } return 0; }
赛时就只做了这些了,I 题因为总罚时忘记加以前的罚时,WA 了
思路:
按题意模拟即可。
#include <bits/stdc++.h> using namespace std; int _, n, m, s, t, k[20], f[20], c[20]; int main() { for (cin >> _; _; _--) { cin >> n >> s >> t >> m; fill(k + 1, k + n + 1, 1e9); fill(f + 1, f + n + 1, 1e9); fill(c + 1, c + n + 1, 0); for (int i = 1, l, p; i <= m; i++) { char z; string r; cin >> l >> z >> r, p = z - 'A' + 1; r == "ac" && (k[p] = min(k[p], l + 20 * c[p])); r == "rj" && (c[p]++); r == "pd" && k[p] == 1e9 && (f[p] = min(f[p], l + 20 * c[p])); } int x = 0, y = 0; for (int i = 1; i <= n; i++) k[i] != 1e9 && (x++, y += k[i]); if (x > s || x == s && y < t) { cout << "0\n"; } else { int z = 0, h = 0; for (int i = 1; i <= n; i++) f[i] != 1e9 && (z++, h += f[i]); if (z < s - x || z == s - x && h + y >= t) { cout << "-1\n"; } else { sort(f + 1, f + n + 1); for (int i = 1; i <= s - x; i++) y += f[i]; cout << s - x + (y >= t) << "\n"; } } } return 0; }
总结:
- 下次写代码要更认真、考虑全面,集中注意力。
- 还要更加及时地补题,把不懂得知识学懂。