“葡萄城杯”牛客周赛 Round 53

我才发现我没发,哈哈,就口胡下贴个代码吧

A题:小红小紫投硬币

概率论考试题)

最简单的方法是令n=1,代特殊值,否则我记得好像挺麻烦的,反正特殊值

#include <bits/stdc++.h>
using namespace std;
int main() {
    cout << 0.5 << endl;
    return 0;
}

B题:小红的字符串

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    string s; cin >> s;
    ll ans = 0;
    int n = s.size();
    s = ' ' + s;
    for (int i = 1; i <= n / 2; i ++ ) {
        if (s[i] == s[n + 1 - i]) continue;
        ans += min(abs(s[i] - s[n + 1 - i]), min(s[i], s[n + 1 - i]) + 26 - max(s[i], s[n + 1 - i]));
    }
    cout << ans << endl;
    return 0;
}

C题:小红的 01 消除 

肯定先消01,然后就可以发现消11,00是没用的了

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; cin >> n;
    string s; cin >> s;
    int x, y, z; cin >> x >> y >> z;
    int j = 0;
    while (j < n && s[j] == '1') j += 1;
    int ans = 0, l = 0;
    for (int i = j; i < n; i ++ ) {
        if (s[i] == '0') {
           while (i < n && s[i] == '0') {
             i += 1;
             l += 1;
           }
           i -= 1;
        }else {
            int cnt = 0;
            while (i < n && s[i] == '1') {
               i += 1;
               cnt += 1;
            }
            i -= 1;
            ans += min(cnt, l);
            if (cnt > l) l = 0;
            else l -= cnt;
        }
    }
    cout << min(ans, y) << endl;
    return 0;
}

D题:小红组比赛

到达型背包

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m; cin >> n >> m;
    vector<int> dp(5010);
    dp[0] = 1;
    for (int i = 0; i < n; i ++ ) {
        vector<int> a(m), backup(5010);
        for (int j = 0; j < m; j ++ ) cin >> a[j];
        for (int k = 0; k <= 5000; k ++ ) {
            if (dp[k]) {
                for (int j = 0; j < m; j ++ ) {
                    backup[k + a[j]] = 1;
                }
                dp[k] = 0;
            }
        }
        dp = backup;
    }
    int t; cin >> t;
    int ans = 1e9 + 7;
    for (int i = 0; i <= 5000; i ++ ) {
        if (dp[i]) {
            ans = min(ans, abs(i - t));
        }
    }
    cout << ans << endl;
    return 0;
}

E题:折半丢弃

贪心,因为有单调性

#include <bits/stdc++.h>
using namespace std;
int main() {
    int tt; cin >> tt;
    while (tt -- ) {
        int n; cin >> n;
        map<int, int> cnt;
        for (int i = 0; i < n; i ++ ) {
            int x; cin >> x;
            cnt[x] += 1;
        }
        int l = 0, r = 1e6 + 7;
        function<bool(int)> check = [&](int x) {
            map<int, int> q = cnt;
            for (auto i = q.rbegin(); i->first >= x; i ++ ) {
                int t = i->first, c = i->second;
                while (t >= x) t /= 2;
                q[t] += c;
            }
            for (int i = x - 1; i >= 0; i -- ) {
                if (!q[i]) return false;
                q[i / 2] += q[i] - 1;
            }
            return true;
        };
        while (l + 1 != r) {
            int mid = l + r >> 1;
            if (check(mid)) l = mid;
            else r = mid;
        }
        cout << l << endl;
    }  
    return 0;
}

F题: 小红走矩阵

卡map访问,挺抽象的

所以就建vis多维吧

#include <bits/stdc++.h>
using namespace std;
using PII = pair<int, int>;
int vis[1001][1001][20][2];
int main() {
    int n, m; cin >> n >> m;
    vector<string> a(n + 1);
    for (int i = 1; i <= n; i ++ ) {
        cin >> a[i], a[i] = ' ' + a[i];
    }
    queue<array<int, 5>> q;
    q.push({1, 1, 15, 0, 0});
    q.push({1, 1, 14, 0, 1});
    q.push({1, 1, 13, 0, 1});
    q.push({1, 1, 11, 0, 1});
    q.push({1, 1, 7, 0, 1});
    vis[1][1][15][0] = vis[1][1][14][1] = vis[1][1][13][1] = vis[1][1][11][1] = vis[1][1][7][1] = 1;
    int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
    int ans = -1;
    while (q.size()) {
        auto t = q.front(); q.pop();
        int x = t[0], y = t[1], state = t[2], step = t[3], ban = t[4];
        if (x == n && y == m) {
            ans = step;
            break;
        }
        for (int i = 0; i < 4; i ++ ) {
            if (state >> i & 1) {
                int xn = x + dx[i], yn = y + dy[i];
                if (xn < 1 || xn > n || yn < 1 || yn > m) continue;
                if (vis[xn][yn][state][ban]) continue;
                if (a[xn][yn] == '.') {
                    vis[xn][yn][state][ban] = 1;
                    q.push({xn, yn, state, step + 1, ban});
                }else {
                    if (!ban) continue;
                    vis[xn][yn][state][0] = 1;
                    q.push({xn, yn, state, step + 1, 0});
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值