結果

問題 No.38 赤青白ブロック
ユーザー lam6er
提出日時 2025-03-31 18:00:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,085 ms / 5,000 ms
コード長 1,991 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 82,592 KB
実行使用メモリ 77,540 KB
最終ジャッジ日時 2025-03-31 18:01:32
合計ジャッジ時間 14,334 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

Kr, Kb = map(int, input().split())
S = input().strip()

R_indices = []
B_indices = []
W_indices = []

for idx, c in enumerate(S):
    if c == 'R':
        R_indices.append(idx)
    elif c == 'B':
        B_indices.append(idx)
    elif c == 'W':
        W_indices.append(idx)

max_len = 0
len_r = len(R_indices)
len_b = len(B_indices)

# Precompute W's indices
W_set = set(W_indices)

for mask_r in range(0, 1 << len_r):
    cnt_r = bin(mask_r).count('1')
    selected_r = [R_indices[i] for i in range(len_r) if (mask_r & (1 << i))]
    r_set = set(selected_r)
    
    for mask_b in range(0, 1 << len_b):
        cnt_b = bin(mask_b).count('1')
        current_total = 10 + cnt_r + cnt_b  # W is always 10
        
        if current_total <= max_len:
            continue
        
        selected_b = [B_indices[i] for i in range(len_b) if (mask_b & (1 << i))]
        b_set = set(selected_b)
        
        merged = sorted(selected_r + selected_b + W_indices)
        
        # Check R constraints
        valid = True
        r_positions = [i for i, pos in enumerate(merged) if pos in r_set]
        r_pos_set = set(r_positions)
        for pos in r_positions:
            left = pos - Kr
            if left in r_pos_set:
                valid = False
                break
            right = pos + Kr
            if right in r_pos_set:
                valid = False
                break
        if not valid:
            continue
        
        # Check B constraints
        b_positions = [i for i, pos in enumerate(merged) if pos in b_set]
        b_pos_set = set(b_positions)
        for pos in b_positions:
            left = pos - Kb
            if left in b_pos_set:
                valid = False
                break
            right = pos + Kb
            if right in b_pos_set:
                valid = False
                break
        if not valid:
            continue
        
        if current_total > max_len:
            max_len = current_total

print(max_len)
0