結果

問題 No.3395 Range Flipping Game
コンテスト
ユーザー SnowBeenDiding
提出日時 2025-12-02 00:31:56
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 1,600 bytes
コンパイル時間 6,080 ms
コンパイル使用メモリ 335,900 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-02 00:32:05
合計ジャッジ時間 9,183 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

#include <atcoder/all>
#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
using namespace atcoder;
using namespace std;

typedef long long ll;

vector<pair<char, int>> run_length_str(string &a) {
    int n = a.size();
    vector<pair<char, int>> ret;
    int now = 1;
    rep(i, 0, n - 1) {
        if (a[i] == a[i + 1])
            now++;
        else {
            ret.push_back(make_pair(a[i], now));
            now = 1;
        }
    }
    ret.push_back(make_pair(a.back(), now));
    return ret;
}

void solve() {
    int n;
    string s;
    cin >> n >> s;
    auto rls = run_length_str(s);
    int m = min((int)rls.size(), 4);
    int cnt = 0;
    set<int> st;
    st.insert(0);
    st.insert(1);
    rep(i, 0, m) {
        cnt += rls[i].second;
        st.insert(cnt - 1);
        st.insert(cnt + 0);
        st.insert(cnt + 1);
    }
    vector<int> idx;
    for (auto v : st) {
        if (0 <= v && v < n) idx.push_back(v);
    }
    auto f = [&](string s) {
        bool isa = false;
        rep(i, 0, n) {
            if (isa && s[i] == 'B') return s;
            if (s[i] == 'A') isa = true;
            s[i] = 'B';
        }
        return s;
    };
    string ans = f(s);
    m = idx.size();
    rep(i, 0, m) rep(j, i, m) {
        string t = s;
        rep(k, idx[i], idx[j] + 1) t[k] ^= 'A' ^ 'B';
        string u = f(t);
        if (u < ans) ans = u;
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(12);
    int t;
    cin >> t;
    while (t--) solve();
}
0