T1:First ABC 2
模拟
代码实现
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; auto i = s.find("ABC"); if (i == string::npos) cout << -1 << '\n'; else cout << i+1 << '\n'; return 0; }
T2:Prefix and Suffix
模拟
代码实现
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string s, t; cin >> s >> t; int ans = 0; if (!t.starts_with(s)) ans += 2; if (!t.ends_with(s)) ans += 1; cout << ans << '\n'; return 0; }
T3:Festival
可以从后往前扫描,对于有烟花的日子,答案为 0
,否则答案为 ans[i+1]+1
代码实现
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; int main() { int n, m; cin >> n >> m; vector<bool> f(n); rep(i, m) { int a; cin >> a; f[a-1] = true; } vector<int> ans(n); for (int i = n-1; i >= 0; --i) { if (f[i]) ans[i] = 0; else ans[i] = ans[i+1]+1; } rep(i, n) cout << ans[i] << '\n'; return 0; }
T4:Polyomino
枚举每个图形的平移和旋转次数即可
代码实现
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; int main() { int n = 3, m = 4; vector p(n, vector<string>(m)); rep(i, n)rep(j, m) cin >> p[i][j]; vector<vector<vector<string>>> s(3); rep(pi, n) { rep(ri, 4) { for (int di = -m; di < m; ++di) { for (int dj = -m; dj < m; ++dj) { vector np(m, string(m, '.')); bool ok = true; rep(i, m)rep(j, m) { if (p[pi][i][j] == '.') continue; int ni = i+di, nj = j+dj; if (ni < 0 or nj < 0 or ni >= m or nj >= m) { ok = false; continue; } np[ni][nj] = '#'; } if (!ok) continue; s[pi].push_back(np); } } { auto pre = p[pi]; rep(i, m)rep(j, m) { p[pi][i][j] = pre[m-1-j][i]; } } } } for (auto p0 : s[0]) { for (auto p1 : s[1]) { for (auto p2 : s[2]) { vector cnt(m, vector<int>(m)); auto put = [&](vector<string> p) { rep(i, m)rep(j, m) { if (p[i][j] == '#') cnt[i][j]++; } }; put(p0); put(p1); put(p2); if (cnt == vector(m, vector<int>(m, 1))) { puts("Yes"); return 0; } } } } puts("No"); return 0; }
T5:Product Development
记 dp[i][a]
表示从前 个计划中选出若干个,使得所有参数为 时的最小代价
这里由于 是 维的,所以我们不妨用 std::map
来维护 dp
代码实现
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; int main() { int n, k, p; cin >> n >> k >> p; map<vector<int>, ll> dp; dp[vector<int>(k)] = 0; rep(i, n) { int c; cin >> c; vector<int> a(k); rep(j, k) cin >> a[j]; auto odd = dp; for (auto [_d, val] : odd) { vector<int> d = _d; rep(j, k) d[j] = min(d[j]+a[j], p); if (dp.count(d)) dp[d] = min(dp[d], val+c); else dp[d] = val+c; } } vector<int> t(k, p); if (dp.count(t)) cout << dp[t] << '\n'; else cout << -1 << '\n'; return 0; }
T6:Vacation Query
延迟线段树的练习题
对于每个点需要维护以下信息:
- 从左端点开始的最长的连续的
0/1
的长度 - 从右端点开始的最长的连续的
0/1
的长度 - 区间中仅包含
0/1
的最长连续子段的长度 - 区间长度
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2022-10-03 间距数列
2022-10-03 1213121