結果

問題 No.1425 Yet Another Cyclic Shifts Sorting
ユーザー shotoyooshotoyoo
提出日時 2022-09-17 00:26:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 1,485 bytes
コンパイル時間 516 ms
コンパイル使用メモリ 87,216 KB
実行使用メモリ 137,952 KB
最終ジャッジ日時 2023-08-23 17:14:21
合計ジャッジ時間 9,527 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
72,256 KB
testcase_01 AC 88 ms
71,784 KB
testcase_02 AC 89 ms
71,776 KB
testcase_03 AC 91 ms
72,052 KB
testcase_04 AC 89 ms
71,936 KB
testcase_05 AC 89 ms
72,056 KB
testcase_06 AC 89 ms
71,568 KB
testcase_07 AC 90 ms
71,948 KB
testcase_08 AC 89 ms
71,992 KB
testcase_09 AC 89 ms
72,196 KB
testcase_10 AC 90 ms
72,216 KB
testcase_11 AC 92 ms
72,200 KB
testcase_12 AC 90 ms
71,952 KB
testcase_13 AC 88 ms
71,752 KB
testcase_14 AC 87 ms
71,988 KB
testcase_15 AC 88 ms
71,776 KB
testcase_16 AC 92 ms
72,220 KB
testcase_17 AC 92 ms
71,848 KB
testcase_18 AC 92 ms
72,216 KB
testcase_19 AC 92 ms
72,120 KB
testcase_20 AC 90 ms
72,208 KB
testcase_21 AC 90 ms
71,572 KB
testcase_22 AC 132 ms
103,688 KB
testcase_23 AC 100 ms
81,420 KB
testcase_24 AC 100 ms
80,168 KB
testcase_25 AC 133 ms
106,752 KB
testcase_26 AC 142 ms
111,080 KB
testcase_27 AC 148 ms
110,528 KB
testcase_28 AC 168 ms
128,752 KB
testcase_29 AC 122 ms
90,936 KB
testcase_30 AC 117 ms
86,908 KB
testcase_31 AC 175 ms
131,248 KB
testcase_32 AC 114 ms
85,396 KB
testcase_33 AC 103 ms
78,104 KB
testcase_34 AC 115 ms
88,140 KB
testcase_35 AC 167 ms
126,808 KB
testcase_36 AC 163 ms
117,260 KB
testcase_37 AC 111 ms
83,368 KB
testcase_38 AC 206 ms
137,668 KB
testcase_39 AC 107 ms
77,932 KB
testcase_40 AC 182 ms
124,864 KB
testcase_41 AC 129 ms
93,592 KB
testcase_42 AC 135 ms
98,100 KB
testcase_43 AC 100 ms
78,888 KB
testcase_44 AC 109 ms
82,676 KB
testcase_45 AC 201 ms
137,952 KB
testcase_46 AC 202 ms
136,800 KB
testcase_47 AC 202 ms
137,688 KB
testcase_48 AC 201 ms
137,328 KB
testcase_49 AC 200 ms
137,420 KB
testcase_50 AC 88 ms
72,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, random
input = lambda : sys.stdin.readline().rstrip()


write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x))
debug = lambda x: sys.stderr.write(x+"\n")
YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO); INF=10**18
LI = lambda : list(map(int, input().split())); II=lambda : int(input())
def debug(_l_):
    for s in _l_.split():
        print(f"{s}={eval(s)}", end=" ")
    print()
def dlist(*l, fill=0):
    if len(l)==1:
        return [fill]*l[0]
    ll = l[1:]
    return [dlist(*ll, fill=fill) for _ in range(l[0])]

def z_algorithm(s):
    """
    各iについてs[0..n)とs[i..n)のLCP(Longest Common Prefix)の長さを求める
    s: str or (list of int )
    """
    n = len(s)
    if n==0:
        return []
    z = [None]*n
    z[0] = 0
    j = 0
    for i in range(1,n):
        k = 0 if j + z[j] <= i else min(j+z[j]-i, z[i-j])
        z[i] = k
        while i+k<n and s[k]==s[i+k]:
            k += 1
        z[i] = k
        if j+z[j] < i+z[i]:
            j = i
    z[0] = n
    return z

n = int(input())
a = list(map(int, input().split()))
sa = sorted(a)
if a==sa:
    ans = 0
else:
    l = 0
    for r in range(n)[::-1]:
        if a[r]!=sa[r]:
            break
    aa = a[l:r+1] + a[l:r+1]
    bb = sa[l:r+1]
    cc = bb + [-1] + aa
    res = z_algorithm(cc)
    for i in range(len(bb)+1, len(res)):
        if res[i]==len(bb):
            ans = 1
            break
    else:
        ans = 2
print(ans)
0