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

D. Linguistics

思路

暴力模拟,这题竟然 2000 2000 2000 分

#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
";
        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
";
    else cout << "NO
";
}
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; 
}
经验分享 程序员 微信小程序 职场和发展