結果

問題 No.1425 Yet Another Cyclic Shifts Sorting
コンテスト
ユーザー LyricalMaestro
提出日時 2026-03-28 13:12:15
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 1,754 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 224 ms
コンパイル使用メモリ 84,992 KB
実行使用メモリ 160,128 KB
最終ジャッジ日時 2026-03-28 13:12:23
合計ジャッジ時間 7,105 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

## https://yukicoder.me/problems/no/1425

import random

H = 9007199254740997

def main():
    N = int(input())
    A = list(map(int, input().split()))

    if N == 2:
        if A[0] <= A[1]:
            print(0)
        else:
            print(1)
        return
    
    # N >= 3

    # 座標圧縮
    a_set = set(A)
    a_list = list(a_set)
    a_list.sort()
    a_map = {}
    for i, a in enumerate(a_list):
        a_map[a] = i
    A = [a_map[A[i]] for i in range(N)]
    a_max = len(a_list) - 1
    BASE = a_max + 3

    # 範囲決める
    B = A.copy()
    B.sort()
    v = None
    for i in reversed(range(N)):
        if v is None:
            if B[i] == A[i]:
                continue
            else:
                v = i
                break
    answer = 2
    if v is None:
        answer = 0
    else:
        C = A[:(i + 1)]
        C += C
        D = B[:(i + 1)]

        # ローリングハッシュ的にやる
        base_hash = 0
        for d in D:
            base_hash *= BASE
            base_hash %= H
            base_hash += d
            base_hash %= H

        h = 0
        for i in range(len(D)):
            h *= BASE
            h %= H
            h += C[i]
            h %= H

        is_ok = False
        if h == base_hash:
            is_ok = True
        bh = pow(BASE, len(D), H)
        for i in range(len(D), 2 * len(D)):
            h *= BASE
            h %= H
            h += C[i]
            h %= H
            
            c = C[i- len(D)]
            c *= bh
            c %= H
            h -= c
            h %= H
            if h == base_hash:
                is_ok = True
        
        if is_ok:
            answer = 1
    print(answer)

        






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