题解:AT_arc157_b [ARC157B] XYYYX

· · 题解

来一个傻子做法。

设 cx 表示字符 X 出现的次数,然后开始分类讨论:

那么剩余的就是代码问题了。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, s, t) for (int i = s; i <= (int)t; i++)
#define pre(i, s, t) for (int i = s; i >= (int)t; i--)
int n, k, cx, cnt, res;
string s;
vector<int> vec;
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> k >> s, s = ' ' + s;
    rep(i, 1, n) cx += (s[i] == 'X');
    if (cx == k) return cout << n - 1, 0;
    if (cx > k) {
        int st = 0, fg = 0;
        if (s[1] == 'Y') st = -1;
        rep(i, 1, n) if (s[i] == 'Y') fg = 1;
        if (!fg) return cout << max(0ll, k - 1), 0;
        rep(i, 1, n - 1) if (s[i] == s[i + 1] && s[i] == 'Y') res++;
        rep(i, 1, n) 
            if (s[i] == 'X') cnt++;
            else if (cnt) {
                if (st == 0) st = cnt;
                else vec.push_back(cnt);
                cnt = 0;
            }
        sort(begin(vec), end(vec));
        for (int x : vec) 
            if (k >= x) res += x + 1, k -= x;
            else res += k, k = 0;
        if (k) res += k;
        cout << max(0ll, res) << '\n';
    } else {
        int st = 0, ed = 0; k -= cx, res = n - 1;
        if (!cx) return cout << max(0ll, res - k), 0;
        if (s[1] == 'X') st = -1;
        rep(i, 1, n)
            if (s[i] == 'Y') cnt++;
            else if (cnt) {
                if (st == 0) st = cnt;
                else vec.push_back(cnt);
                cnt = 0;
            }
        if (cnt) {
            if (st == -1) st = cnt, ed = -1;
            else ed = cnt;
        } else ed = -1;
        sort(begin(vec), end(vec), greater<int>());
        if (st != -1) {
            if (k >= st) k -= st, res -= st;
            else res -= k, k = 0;
        }
        if (ed != -1) {
            if (k >= ed) k -= ed, res -= ed;
            else res -= k, k = 0;
        }
        for (int x : vec) 
            if (k >= x) res = res - x - 1, k -= x;
            else if (k) res = res - k - 1, k = 0;
        cout << res << '\n';
    }
    return 0;
}

后记:我们打 ACM 时候有人抄这一道题的题解,真搞不懂为啥这种傻子题也会有人抄题解。。。