Codeforces Round #794 (Div. 2) D. Linguistics

博客围绕 D. Linguistics 展开,给出解题思路为暴力模拟,还提及该题分值为 2000 分,涉及 C++ 与算法相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Linguistics

思路

暴力模拟,这题竟然 200020002000

#include <bits/stdc++.h>
using namespace std;
#define PB push_back
typedef vector<int> VI;
int a, b, c, d;
VI res, ab, ba;
void work(int num, int f) {
    if (num & 1) {
       res.PB(num); 
    } else {
        if (f) ab.PB(num);
        else ba.PB(num);
    }
}
void slove() {
    cin >> a >> b >> c >> d;
    string str;
    cin >> str;
    int suma = 0, sumb = 0;
    for (int i = 0; i < str.size(); i ++ )
        if (str[i] == 'A') suma ++;
        else sumb ++;
    if (suma != a + c + d || sumb != b + c + d) {
        cout << "NO\n";
        return;
    }
    int num = 0, f = 0;
    for (int i = 1; i < str.size(); i ++ ) {
        if (str[i] != str[i - 1]) {
            if (!num) {
                num = 2;
                if (str[i - 1] == 'A') f = 1;
                else f = 0;
            } else num ++;
        } else if (num && str[i] == str[i - 1]) {
            work(num, f);
            num = 0;
        }
    }
    if (num) work(num, f);
    sort(ab.begin(), ab.end()), sort(ba.begin(), ba.end());
    for (auto t : ab) {
        int tmp = t;
        if (tmp / 2 > c) {
            tmp -= 2 * c;
            c = 0;
        } else {
            c -= tmp / 2;
            tmp = 0;
        }
        if (tmp - 2 > 0) d = max(0, d - (tmp - 2) / 2);
    }
    for (auto t : ba) {
        int tmp = t;
        if (tmp / 2 > d) {
            tmp -= 2 * d;
            d = 0;
        } else {
            d -= tmp / 2;
            tmp = 0;
        }
        if (tmp - 2 > 0) c = max(0, c - (tmp - 2) / 2);
    }
    for (auto t : res) {
        int tmp = t;
        if (tmp / 2 > c) {
            tmp -= c * 2;
            c = 0;
        } else {
            c -= tmp / 2;
            tmp = 0;
        }
        d = max(0, d - tmp / 2);
    }
    if (c == 0 && d == 0) cout << "YES\n";
    else cout << "NO\n";
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        res.clear(), ab.clear(), ba.clear();
        slove();
    }
    return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值