提出 #72068732


ソースコード 拡げる

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define vi vector <int>
#define sz(a) ((int) (a).size())
#define me(f, x) memset(f, x, sizeof(f))
#define uint unsigned int
#define pb emplace_back
using namespace std;
const int N = 1e6 + 7, mod = 998244353;

int n;
vi compute_next(int a) {
    vi outs;
    L(sub, 0, (1 << n) - 1) {
        vi pos;
        vi npos;
        L(d, 0, n - 1)
            if(sub >> d & 1)
                pos.pb(d);
            else 
                npos.pb(d);
        int res = 0;
        L(o, 0, 1) {
            L(i, 0, sz(pos) - 1)
                if(a >> pos[i] & 1)
                    res |= 1 << pos[sz(pos) - i - 1];
            swap(pos, npos);
        }
        outs.pb(res);
    }
    return outs;
}
vi operate(vi a, string s) {
    vi pos;
    vi npos;
    L(d, 0, n - 1)
        if(s[d] == 'A')
            pos.pb(d);
        else 
            npos.pb(d);
    vi b(n);
    int res = 0;
    L(o, 0, 1) {
        L(i, 0, sz(pos) - 1)
            if(a[pos[i]])
                b[pos[sz(pos) - i - 1]] = 1;
        swap(pos, npos);
    }
    return b;
}

bool inc(vi a) {
    L(i, 1, n - 1) if(a[i - 1] > a[i])return false;
    return true;
}

vector<string>solve(vi a){
    assert(n == sz(a));
    int cnt1 = 0;
    L(i, 0, sz(a) - 1) cnt1 += a[i];
    if(cnt1 > n / 2) {
        vi b = a;
        reverse(b.begin(), b.end());
        for(auto&u : b)
            u = 1 - u;
        auto ns = solve(b);
        for(auto&p : ns)
            reverse(p.begin(), p.end());
        return ns;
    }
    vector<string>ans;
    if(inc(a)) {
        return ans;
    }
    int ok_1 = true;
    int pref_1 = 0;
    int suf_0 = 0;
    while(a[pref_1] == 1) ++pref_1;
    while(a[n - suf_0 - 1] == 0) ++suf_0;
    int num = 0;
    L(i, pref_1, n - cnt1 - 1)
        num += a[i];
    //cout << num << ',' << suf_0 << endl;
    if(num <= suf_0) {
        string ret;
        L(t, 1, n) ret += "A";
        int qwq = cnt1 - pref_1;
        L(i, pref_1, n - 1) if(a[i] == 1) ret[i] = 'B'; 
        L(i, n - num, n - 1) ret[i] = 'B';  
        //cout << "RET = " << ret << endl;
        ans.pb(ret);
        vi na = operate(a, ret);
        if(!inc(na)) assert(false);
        return ans;
    }
    string ret;
    int cnt[2] = {0, 0};
    int half = n / 2;
    L(i, 0, half - 1)
        if(a[i])
            ++cnt[1], ret += "B";
        else 
            ++cnt[0], ret += "A";
    if(n & 1)
        ret += "B";
    L(i, 1, cnt[1])
        ret += "B";
    L(i, 1, cnt[0])
        ret += "A";
    vi b = operate(a, ret);
    //cout << "ret = " << ret << endl;
    //for(auto&u : b)
    //    cout << u << ' ';
    //cout << endl;
    ans=solve(b);
    
    ans.insert(ans.begin(), ret);
    return ans;
}
queue<int>q;
int dis[N];
void sanity() {
    n = 10;
    L(i, 0, (1 << n) - 1) dis[i] = n + 1;
    L(i, 0, n) {
        dis[(1 << n) - (1 << i)] = 0;
        q.push((1 << n) - (1 << i));
    }
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        auto nw = compute_next(u);
        for(auto&p : nw) {
            if(dis[p] > dis[u] + 1) {
                dis[p] = dis[u] + 1;
                q.push(p);
            }
        }
    }
    cout << *max_element(dis, dis + (1 << n)) << '\n';
}
int main () {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
    //mt19937 rng;
    //n = 9;
    //L(t, 1, 2000) {
    //    vi a(n);
    //    L(i, 0, n - 1) a[i] = rng() & 1;
    //    cout << "a = " << endl;
    //    int cnt1 = 0;
    //    L(i, 0, n - 1)
    //        cnt1 += a[i];
    //    auto ns = solve(a);
    //    for(auto&p : ns)
    //        a = operate(a, p);
    //    assert(inc(a));
    //    cout << "AFTER " << t << endl;
    //}
    int t; 
    cin >> t; 
    while(t--) {
        cin >> n;
        vi a(n);
        L(i, 0, n - 1) cin >> a[i];
        auto ans = solve(a);
        cout << sz(ans) << '\n';
        for(auto&u : ans){
            cout << u << '\n';
        }
    }
    return 0;
} 

提出情報

提出日時
問題 B - Split and Reverse
ユーザ zhoukangyang
言語 C++23 (GCC 15.2.0)
得点 1100
コード長 4246 Byte
結果 AC
実行時間 27 ms
メモリ 14936 KiB

コンパイルエラー

./Main.cpp: In function 'std::vector<int> operate(std::vector<int>, std::string)':
./Main.cpp:44:9: warning: unused variable 'res' [-Wunused-variable]
   44 |     int res = 0;
      |         ^~~
./Main.cpp: In function 'std::vector<std::__cxx11::basic_string<char> > solve(std::vector<int>)':
./Main.cpp:89:13: warning: unused variable 'qwq' [-Wunused-variable]
   89 |         int qwq = cnt1 - pref_1;
      |             ^~~
./Main.cpp:77:9: warning: unused variable 'ok_1' [-Wunused-variable]
   77 |     int ok_1 = true;
      |         ^~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 1100 / 1100
結果
AC × 1
AC × 21
セット名 テストケース
Sample 00-sample-001.txt
All 00-sample-001.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt
ケース名 結果 実行時間 メモリ
00-sample-001.txt AC 2 ms 3504 KiB
01-001.txt AC 21 ms 3632 KiB
01-002.txt AC 27 ms 3580 KiB
01-003.txt AC 27 ms 3624 KiB
01-004.txt AC 11 ms 6216 KiB
01-005.txt AC 18 ms 12396 KiB
01-006.txt AC 18 ms 12164 KiB
01-007.txt AC 19 ms 12584 KiB
01-008.txt AC 20 ms 12972 KiB
01-009.txt AC 20 ms 12288 KiB
01-010.txt AC 21 ms 14936 KiB
01-011.txt AC 20 ms 14660 KiB
01-012.txt AC 18 ms 13988 KiB
01-013.txt AC 17 ms 14352 KiB
01-014.txt AC 9 ms 8276 KiB
01-015.txt AC 14 ms 4472 KiB
01-016.txt AC 14 ms 4400 KiB
01-017.txt AC 13 ms 11416 KiB
01-018.txt AC 13 ms 9440 KiB
01-019.txt AC 18 ms 12724 KiB
01-020.txt AC 19 ms 14680 KiB