Codeforces Round 933 (Div. 3)

在这里插入图片描述

Codeforces Round 933 (Div. 3)

Codeforces Round 933 (Div. 3)

A. Rudolf and the Ticket

题意:俩口袋各有n和m枚不同面值的硬币,各取一枚,有多少种小于k的可能。

思路:数据很小,暴力枚举。

AC code:

void solve() {
    cin >> n >> m >> k;
    int ans = 0;
    for (int i = 0; i < n; i ++) cin >> b[i];
    for (int i = 0; i < m; i ++) cin >> c[i];
 
    for (int i = 0; i < n; i ++)
        for (int j = 0; j < m; j ++) {
            if (b[i] + c[j] <= k) ans ++;
         }
         cout << ans << endl;
}

B. Rudolf and 121

题意:一组长度为n的非负整数序列,每次操作可以选择下标为i的元素-2,同时(i - 1)的元素-1,(i + 1)的元素-1,是否可能使得序列的元素全部为0。

思路:从i=2开始枚举,每次都清空前一个数,出现矛盾(负数)直接退出,注意最后需要判断n和n-1两个位置的数是否为0。

AC code:

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    for (int i = 2; i < n; i ++) {
        int x = a[i - 1];
        if (x * 2 > a[i]) {
            cout << "NO" << endl;
            return;
        } else {
            a[i] -= x * 2;
            a[i - 1] = 0;
            a[i + 1] -= x;
            if (a[i + 1] < 0) {
                cout << "NO" << endl;
                return;
            }
        }
    } 
    for (int i = 1; i <= n; i ++) {
        if (a[i] != 0) {
            cout << "NO" << endl;
            return;
        }
    }
    cout << "YES" << endl;
}

C. Rudolf and the Ugly String

题意:字符串中有连续的"pie"或者"map"则不优美,每次可以删除一个字符,最少删几个整个字符串优美。

思路:每存在一个不优美的词答案+1,特殊情况"mapie"出现时,只需要操作一次即可。

AC code:

void solve() {
    cin >> n;
    string s; cin >> s;
    string x = "pie", y = "map";
    if (s.find(x) == -1 && s.find(y) == -1) {
        cout << "0" << endl;
        return;
    }
    int cnt = 0;
    for (int i = 0; i < n; i ++) {
        if (s[i] == 'm' && s[i + 1] == 'a' && s[i + 2] == 'p') {
            i += 2;
            cnt ++;
        } 
        if (s[i] == 'p' && s[i + 1] == 'i' && s[i + 2] == 'e') {
            i += 2;
            cnt ++;
        }
    }
    for (int i = 0; i < n; i ++) {
        if (s[i] == 'm' && s[i + 1] == 'a' && s[i + 2] == 'p' && s[i + 3] == 'i' && s[i + 4] == 'e') cnt --;
    }
    cout << cnt << endl;
}

D. Rudolf and the Ball Game

题意:n个人围城一圈扔球,现在从x人开始,已知m轮,每轮扔的距离已知,但方向可能知道可能不知道,不知道的话可能是顺时针也可能是逆时针,m轮后球可能在哪几个人手里。

思路:这个数据范围首先就可以暴力。

开一个set来记录答案,每次取出当前球可能在的人,然后顺时针逆时针的可能性再全部压入,最后得到的就是最终球可能在的人。

AC code:

void solve() {
    cin >> n >> m >> x;
    vector<pair<int, char>> p(m);
    for (int i = 0; i < m; i ++) 
        cin >> p[i].first >> p[i].second;
    int now = x;
    set<int> ans;
    ans.insert(x);
    for (auto [t, pos] : p) {
        set<int> se;
        if (pos == '0') {
            for (auto ca : ans) {
                int u = (ca + t - 1) % n + 1;
                se.insert(u);
            }
        } else if (pos == '1') {
            for (auto ca : ans) {
                int v = (ca + n - t - 1) % n + 1;
                se.insert(v);
            }
        } else if (pos == '?'){
            for (auto ca : ans) {
                int u = (ca + t - 1) % n + 1;
                int v = (ca + n - t - 1) % n + 1;
                se.insert(u);
                se.insert(v);
            }
        }
        ans = se;
    }
    cout << ans.size() << endl;
    for (auto t : ans) cout << t << " ";
    cout << endl;
}

E. Rudolf and k Bridges

题意:n*m的河需要建造连续的k行桥,每行桥要跨越m列,首尾建造桥墩,中间需要也需要建造桥墩,桥墩间的距离要<=d+1,桥墩建造的成本和当前河的深度挂钩, 成本最低为多少。

思路:

首先对于每一行需要求出对当前行建造桥的最低成本,然后再枚举连续k行中最少的成本;

对于每一行需要用dp去求取最低成本,同时需要保证桥墩间的距离<= d+1,可以通过单调队列来同时保证距离和当前最低成本;

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define db double
#define pb push_back
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 2001;
const int INF = 0x3f3f3f3f3f, MOD = 998244353;
int T, n, m, k, d;

void solve() {
    cin >> n >> m >> k >> d;
    vector<vector<int>> a(n + 1, vector<int> (m + 1));
    for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++) 
        cin >> a[i][j];
    vector<int> b;
    b.pb(0);
    for (int i = 1; i <= n; i ++) {
        vector<int> dp(m + 1);
        deque<int> q;
        q.push_back(1);
        dp[1] = 1;
        for (int j = 2; j <= m; j ++) {
            while (!q.empty() && j - q.front() > d + 1) q.pop_front();
            dp[j] = dp[q.front()] + a[i][j] + 1;
            while (!q.empty() && dp[j] <= dp[q.back()]) q.pop_back();
            q.push_back(j);
        }
        b.pb(dp[m]);
    }
    vector<int> sum(n + 1, 0);
    for (int i = 1; i <= n; i ++) {
        sum[i] = sum[i - 1] + b[i];
    }
    int ans = INF;
    for (int i = k; i <= n; i ++) {
        ans = min(ans, sum[i] - sum[i - k]);
    }
    cout << ans << endl;
}

signed main() {
    fast();
    
    T = 1;
    cin >> T;
    while (T --) {
        solve();
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值