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] 表示从前 i 个计划中选出若干个,使得所有参数为 a=(a1,a2,,ak) 时的最小代价

这里由于 ak 维的,所以我们不妨用 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 的最长连续子段的长度
  • 区间长度