A-雾之湖的冰精
大于9输出,否则输出。
#include<bits/stdc++.h>
using namespace std;
void solve(){
int a, b;
cin >> a >> b;
if (a + b <= 9) cout << "Yes";
else cout << "No";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while(T--){
solve();
}
return 0;
}
B-博丽神社的巫女
排序后找到小于等于的最大值,可以二分找,但是数据少直接暴力去找到即可。
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
int cnt = 0, p = 0;
for (int i = 0; i < n; i++){
if (a[i] <= x){
cnt++;
p = a[i];
} else break;
}
cout << cnt << ' ' << x - p;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while(T--){
solve();
}
return 0;
}
C-红魔馆的馆主
暴力枚举
先算出的值,因为495只有三位数字所以在结尾加的数不可能超过495,直接从0-495枚举加到结尾后合法的数即可。需要注意有前导0的情况,比如可能加1不合法,但是加01是合法的(昨天的用例是不存在这种情况的,今天重新交发现过不了了)。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
ll n;
cin >> n;
ll res = n % 495;
if (!res){
cout << -1;
return;
}
for (int i = 0; i < 10; i++){
if ((res * 10 + i) % 495 == 0){
cout << i;
return;
}
}
for (int i = 1; i < 100; i++){
if ((res * 100 + i) % 495 == 0){
if (i < 10) cout << 0;
cout << i;
return;
}
}
for (int i = 1; i < 495; i++){
if ((res * 1000 + i) % 495 == 0){
if (i < 100) cout << 0;
if (i < 10) cout << 0;
cout << i;
return;
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while(T--){
solve();
}
return 0;
}
D-迷途之家的大贤者
贪心
首先思考要是字符串为的情况下,因为是小红先手,所以剩下的肯定是。再思考,结果还是和前面一样的,小红直接删除子串,留下。如果是,小红先手删除,剩下,小紫删除,剩下,答案还是一样的。如果是的情况下,小红先手不删,那小紫删除留下,不符合题目中,两个人都选择最优决策。在中间继续加字母的情况还是一样的,所以最后的答案就是第一个字符和最后一个字符中的最大值。
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
string s;
cin >> s;
cout << max(s[0], s[n - 1]);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while(T--){
solve();
}
return 0;
}
E-魔法之森的蘑菇
BFS
要求最短的走法,考虑用写。这个题和走迷宫求最少步数的区别,就是只有遇到了蘑菇才能转向。在传统的算法中,我们在每一个位置都能转向,所以所有能走的位置都可以加入队列中。这个题只要加一个向某一位置一直前进,再限制一下只有在蘑菇的位置,才能将该位置加入到队列中即可。
#include<bits/stdc++.h>
using namespace std;
typedef array<int, 3> arr;
typedef pair<int, int> PII;
const int N = 1010;
char mp[N][N];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void solve(){
int n, m;
cin >> n >> m;
PII S, T;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
if (mp[i][j] == 'S') S = {i, j};
if (mp[i][j] == 'T') T = {i, j};
}
}
int ans = 1e9;
auto check = [&](int x, int y){
if (x < 1 || x > n || y < 1 || y > m) return false;
return true;
};
queue<arr> q;
q.push({S.first, S.second, 0});
vector vis(n + 5, vector<int>(m + 5, 1e9));
vis[S.first][S.second] = 0;
while(q.size()){
auto [x, y, d] = q.front();
q.pop();
for (int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i], dt = d;
//如果没有遇到障碍物或者是蘑菇并且没有走出森林,一直向同一个方向走
while (mp[nx][ny] == '.' && check(nx, ny)){
nx += dx[i], ny += dy[i], dt++;
}
//因为遇到了障碍物或者蘑菇才会退出while,所以(nx,ny)还在森林中就一定是蘑菇
if (check(nx, ny) && mp[nx][ny] != '#' && dt + 1 < vis[nx][ny]){
if (make_pair(nx, ny) != T){
vis[nx][ny] = dt + 1;
q.push({nx, ny, dt + 1});
} else{ //终点不加入队列,防止死循环
ans = min(ans, dt + 1);
}
}
}
}
if (ans != 1e9) cout << ans << '\n';
else cout << "-1\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
cin >> T;
while (T--){
solve();
}
return 0;
}
F-三途川的摆渡人
动态规划
我觉得这个题反而比E题简单。因为是不超过200的(今天已经改成127了),因为是按位与运算所以不可能得出一个超过最大值的数,所以按位与运算能得到的数的范围是0~127。设按位与之后结果为最少需要个数,先枚举每一个,再去枚举0~127中已经出现过的结果将其与相与,可以得到。所以可以从相与结果为的数与相与得到,需要更新的值,。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int f[500];
void solve(){
memset(f, 0x3f, sizeof f);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int res = a[0];
for (int i = 0; i < n; i++) {
res &= a[i];
if (a[i] == 0){
cout << n - 1 << '\n';
return;
}
}
if (res){
cout << "-1\n";
return;
}
for (int i = 0; i < n; i++){
f[a[i]] = 1;
for (int j = 0; j <= 127; j++){
if (f[j] != 0x3f3f3f3f){
f[a[i] & j] = min(f[a[i] & j], f[j] + 1);
}
}
}
if (f[0] == 0x3f3f3f3f) cout << "-1\n";
else cout << n - f[0] << '\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
cin >> T;
while(T--){
solve();
}
return 0;
}