結果

問題 No.3219 Ruler to Maximize
ユーザー asmin
提出日時 2025-08-01 22:06:47
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,098 bytes
コンパイル時間 5,507 ms
コンパイル使用メモリ 216,480 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-08-01 22:06:56
合計ジャッジ時間 8,920 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 14 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct UnionFind{
    vector<int> par, rank, siz;
    UnionFind(int n): par(n, -1), rank(n, 0), siz(n, 1){}
    // x の根を求める
    int root(int x){
        if(par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    // x と y が連結か
    bool same(int x, int y){
        return root(x) == root(y);
    }
    // x と y を結ぶ
    bool merge(int x, int y){
        int rx = root(x), ry = root(y);
        if(rx == ry) return false;
        if(rank[rx] < rank[ry]) swap(rx, ry);
        par[ry] = rx;
        if(rank[rx] == rank[ry]) ++rank[rx];
        siz[rx] += siz[ry];
        return true;
    }
    // x の連結成分の個数
    int size(int x){
        return siz[root(x)];
    }
};


void solve(){
    int N; cin >> N;
    UnionFind uf(16);
    vector<bool> ok(16, false);
    vector<int> A(N);
    for(int i = 0; i < N; ++i){
        cin >> A[i];
        int c = -1;
        for(int j = 0; j < 16; ++j){
            if((A[i] >> j) & 1){
                ok[j] = true;
                if(c == -1){
                    c = j;
                }else{
                    uf.merge(c, j);
                }
            }
        }
    }
    int B = 0, W = 0;
    int g = -1;
    vector<int> k(15, 0);
    for(int i = 15; i >= 0; --i){
        if(ok[i]){
            if(g == -1){
                B += (1 << i);
                g = i;
                k[i] = 1;
            }else if(uf.same(g, i)){
                B += (1 << i);
                k[i] = 1;
            }else{
                W += (1 << i);
            }
        }
    }
    cout << B * W << "\n";
    if(B * W == 0){
        for(int i = 0; i < N; ++i) cout << "B";
        cout << "\n";
    }else{
        for(int i = 0; i < N; ++i){
            if((A[i] >> g) & 1) cout << "B";
            else cout << "W";
        }
        cout << "\n";
    }
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << setprecision(10) << fixed;


    int T;
    T = 1;
    //cin >> T;
    for(;T--;) solve();
}

0