結果

問題 No.2240 WAC
ユーザー lam6er
提出日時 2025-03-20 20:30:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 1,558 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 82,612 KB
実行使用メモリ 99,876 KB
最終ジャッジ日時 2025-03-20 20:31:20
合計ジャッジ時間 4,316 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    N = int(data[0])
    M = int(data[1])
    S = data[2]

    W_pos = []
    A_pos = []
    C_pos = []
    for idx, c in enumerate(S):
        if c == 'W':
            W_pos.append(idx)
        elif c == 'A':
            A_pos.append(idx)
        elif c == 'C':
            C_pos.append(idx)

    # Step 1: Process W's in reverse order
    W_sorted = sorted(W_pos, reverse=True)
    A_sorted = sorted(A_pos)
    C_sorted = sorted(C_pos)

    j = len(A_sorted) - 1  # pointer for available A's in A_sorted
    possible = True
    for w in W_sorted:
        # Binary search for the first A >= w
        idx = bisect.bisect_left(A_sorted, w)
        # Find the rightmost A >= w that is <= j
        if idx > j:
            possible = False
            break
        # Select the smallest index >= idx that is <= j
        if idx <= j:
            j -= 1
        else:
            possible = False
            break

    if not possible:
        print("No")
        return

    remaining_A = A_sorted[:j+1]
    if len(remaining_A) < M:
        print("No")
        return

    # Now check AC pairs
    ptr_c = 0
    count = 0
    for a in remaining_A:
        while ptr_c < len(C_sorted) and C_sorted[ptr_c] < a:
            ptr_c += 1
        if ptr_c < len(C_sorted):
            count += 1
            ptr_c += 1
        if count == M:
            break

    if count >= M:
        print("Yes")
    else:
        print("No")

if __name__ == '__main__':
    main()
0