A题: 雾之湖的冰精
思路: 签到题
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int a, b; cin >> a >> b;
if (a + b > 9) cout << "No\n";
else cout << "Yes\n";
return 0;
}
B题: 博丽神社的巫女
思路: 算出有多少个数比 x 小即可,期间用min更新cost剩余
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, x; cin >> n >> x;
int cnt = 0, cost = x;
while (n -- ) {
int a; cin >> a;
if (x >= a) {
cnt ++;
cost = min(cost, x - a);
}
}
cout << cnt << ' ' << cost << endl;
return 0;
}
C题: 红魔馆的馆主
思路:维护余数
对于给定的n,如果余数刚好为0,根据题意直接输出-1。
否则,最多在后面补充3位数,使得能够被整除。因为最后的答案为n * 10^t + x,而n * 10^t 与495的余数在3位以内,所以x最多贡献3位,使得最后的余数为0。(暴力枚举后面添加的x即可)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n; cin >> n;
if (n % 495 == 0) return cout << -1 << endl, 0;
for (int i = 0; i <= 9; i ++ )
if ((10 * n + i) % 495 == 0) {
cout << i << endl;
return 0;
}
for (int i = 0; i <= 9; i ++ )
for (int j = 0; j <= 9; j ++ ) {
if ((100 * n + 10 * i + j) % 495 == 0) {
cout << i << j << endl;
return 0;
}
}
for (int i = 0; i <= 9; i ++ )
for (int j = 0; j <= 9; j ++ )
for (int k = 0; k <= 9; k ++ ) {
if ((1000 * n + 100 * i + 10 * j + k) % 495 == 0) {
cout << i << j << k << endl;
return 0;
}
}
return 0;
}
D题: 迷途之家的大贤者
思路: 博弈论
结论:取末尾或者开头更大的那一个
显然,最后只会剩下一个字符。
且两者都在维护字符串的两端最小或者最大。
如果一开始,最大的字符出现在字符串的两端,那么小红一定能删去其余的字符,取到这个字符作为答案。否则,最大的字符出现在字符串的中间,记开头的字符小于等于末尾的字符,那么小红会从开头开始删,一直删到开头的字符大于末尾字符。(如果小红选择删去中间,那么小紫可以取开头作为最后的答案,该答案小于末尾字符,那还不如直接删成只剩最后一个字符)。这么做会使小紫去删开头的字符,一直删到开头的字符小于末尾的字符,否则小红可以取到开头的字符作为答案(该答案大于末尾字符)。这样循环操作,使得只剩下了最后的字符,也就是末尾字符。
我们一开始假设的是开头小于等于末尾,那么只要max取开头或者结尾最大即可。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
string s; cin >> s;
cout << max(s[0], s[n - 1]) << endl;
return 0;
}
E题: 魔法之森的蘑菇
思路:bfs(但多了一个维度维护方向)
array可以存三个变量,方便bfs中的queue弹出
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1};
inline void solve() {
int n, m; cin >> n >> m;
vector<string> v(n + 1);
for (int i = 1; i <= n; i ++ ) cin >> v[i], v[i] = " " + v[i];
queue<array<int, 3>> q;
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(m + 1, vector<int>(4, 1e9 + 7)));
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= m; j ++ ) {
if (v[i][j] == 'S') {
for (int k = 0; k < 4; k ++ ) {
q.push({i, j, k});
dp[i][j][k] = 0;
}
break;
}
}
}
while (q.size()) {
auto p = q.front(); q.pop();
int x = p[0], y = p[1], d = p[2];
if (v[x][y] == '*') {
for (int i = 0; i < 4; i ++ ) {
if (i == (d ^ 3)) continue;
int x1 = x + dx[i], y1 = y + dy[i];
if (x1 < 1 || x1 > n || y1 < 1 || y1 > m || v[x1][y1] == '#' || dp[x1][y1][i] != 1e9 + 7) continue;
dp[x1][y1][i] = dp[x][y][d] + 1;
q.push({x1, y1, i});
}
}else {
int x1 = x + dx[d], y1 = y + dy[d];
if (x1 < 1 || x1 > n || y1 < 1 || y1 > m || v[x1][y1] == '#' || dp[x1][y1][d] != 1e9 + 7) continue;
dp[x1][y1][d] = dp[x][y][d] + 1;
q.push({x1, y1, d});
}
}
int ans = 1e9 + 7;
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= m; j ++ ) {
if (v[i][j] == 'T') {
for (int k = 0; k < 4; k ++ ) {
ans = min(ans, dp[i][j][k]);
}
break;
}
}
}
if (ans == 1e9 + 7) cout << -1 << endl;
else cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int _; cin >> _;
while (_ -- ) solve();
return 0;
}
F题: 三途川的摆渡人
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int INF = 1e9 + 7;
inline void solve() {
int n; cin >> n;
vector<int> a(n);
vector<int> dp(128, INF);
for (int i = 0; i < n; i ++ ) cin >> a[i];
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
int m = a.size();
for (int i = 0; i < m; i ++ ) {
dp[a[i]] = min(dp[a[i]], 1);
for (int j = 0; j <= 127; j ++ ) {
dp[a[i] & j] = min(dp[a[i] & j], dp[j] + 1);
}
}
if (dp[0] == INF) cout << -1 << endl;
else cout << n - dp[0] << endl;
}
int main() {
int _; cin >> _;
while (_ -- ) solve();
return 0;
}