牛客周赛 Round 37

A-雾之湖的冰精

a+b大于9输出No,否则输出Yes

#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-博丽神社的巫女

排序后找到小于等于x的最大值,可以二分找,但是数据少直接暴力去找到即可。

#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-红魔馆的馆主

暴力枚举

先算出n\%495的值,因为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-迷途之家的大贤者

贪心

首先思考要是字符串为ab的情况下,因为是小红先手,所以剩下的肯定是b。再思考aab,结果还是和前面一样的,小红直接删除子串aa,留下b。如果是azb,小红先手删除a,剩下zb,小紫删除z,剩下b,答案还是一样的。如果是的azb情况下,小红先手不删,那小紫删除zb留下a,不符合题目中,两个人都选择最优决策。在中间继续加字母的情况还是一样的,所以最后的答案就是第一个字符和最后一个字符中的最大值。

#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

要求最短的走法,考虑用bfs写。这个题和bfs走迷宫求最少步数的区别,就是只有遇到了蘑菇才能转向。在传统的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题简单。因为a_i是不超过200的(今天已经改成127了),因为是按位与运算所以不可能得出一个超过最大值的数,所以按位与运算能得到的数的范围是0~127。设按位与之后结果为i最少需要f[i]个数,先枚举每一个a_i,再去枚举0~127中已经出现过的结果(j)将其与a_i相与,可以得到j\&a_i。所以可以从相与结果为j的数与a_i相与得到j\&a_i,需要更新f[j\&a_i]的值,f[j\&a_i]=min(f[j\&a_i],f[j]+1)

#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;
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值