CF2232C2题解

传送门:https://www.luogu.com.cn/problem/CF2232C2
简单版本:https://www.luogu.com.cn/problem/CF2232C1

原来的枚举思路肯定不能优化了。因为 \(I\)\(E\) 代价和价值都是 \(1\),考虑反悔贪心。

我们在保证当前 \(E\) 都能入座的情况下,留出尽量多的空桌子给 \(I\)

如果发现新的 \(E\) 无法入座时,由于 \(I\)\(E\) 价值代价都相同,我们优先考虑 \(E\),反悔之前的决策,如果可以则多开一张桌子给之前一个非单独的 \(A\),腾出位置让新的 \(E\) 入座一张有人的桌子。

维护的东西和简单版本几乎一模一样,再单独维护非单独的 \(A\) 的数量让新的 \(E\) 来替换即可。时间复杂度 \(O(n)\)

#include <bits/stdc++.h>

const int N = 5e5 + 1;

using namespace std;

char a[200002];

void solve() {
    int n, x, s;
    cin >> n >> x >> s;
    cin >> a + 1;
    int A = 0, v = 0, now = 0, ans = 0;
    for (int i = 1; i <= n; ++i) {
        if (a[i] == 'A') {
            if (v) {
                --v;
                ++A;
                ++ans;
            } else if (now != x) {
                ++now;
                v += s - 1;
                ++ans;
            }
        } else if (a[i] == 'I') {
            if (now != x) {
                ++now;
                v += s - 1;
                ++ans;
            }
        } else {
            if (v) --v, ++ans;
            else if (A && now != x) {
                ++now;
                --A;
                v += s - 1;
                ++ans;
            }
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}
posted @ 2026-06-15 03:36  Jefferyzzzz  阅读(3)  评论(0)    收藏  举报