結果

問題 No.2254 Reverse Only
ユーザー lam6er
提出日時 2025-04-16 15:59:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,851 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 81,500 KB
実行使用メモリ 165,184 KB
最終ジャッジ日時 2025-04-16 16:01:21
合計ジャッジ時間 7,626 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N, k = int(input[ptr]), int(input[ptr+1])
    ptr +=2
    A = list(map(int, input[ptr:ptr+N]))
    ptr +=N
    B = list(map(int, input[ptr:ptr+N]))
    
    if k == 1:
        if A == B:
            print("Yes")
        else:
            print("No")
        return
    
    if k > N:
        if A == B:
            print("Yes")
        else:
            print("No")
        return
    
    # Check if element counts are the same
    count_A = defaultdict(int)
    count_B = defaultdict(int)
    for a in A:
        count_A[a] +=1
    for b in B:
        count_B[b] +=1
    if count_A != count_B:
        print("No")
        return
    
    # If k is even, no further checks needed
    if k % 2 == 0:
        print("Yes")
        return
    
    # For odd k, check odd/even positions
    a_odd = defaultdict(int)
    a_even = defaultdict(int)
    b_odd = defaultdict(int)
    b_even = defaultdict(int)
    
    for i in range(N):
        a = A[i]
        if (i+1) % 2 == 1:
            a_odd[a] +=1
        else:
            a_even[a] +=1
        
        b = B[i]
        if (i+1) % 2 == 1:
            b_odd[b] +=1
        else:
            b_even[b] +=1
    
    # Check all elements in a_odd and a_even
    for x in a_odd:
        if a_odd[x] != b_odd.get(x, 0):
            print("No")
            return
    for x in a_even:
        if a_even[x] != b_even.get(x, 0):
            print("No")
            return
    
    # Check elements in b_odd not in a_odd
    for x in b_odd:
        if x not in a_odd and b_odd[x] >0:
            print("No")
            return
    for x in b_even:
        if x not in a_even and b_even[x] >0:
            print("No")
            return
    
    print("Yes")

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