結果

問題 No.1425 Yet Another Cyclic Shifts Sorting
ユーザー sgsw
提出日時 2021-08-15 00:38:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 1,407 bytes
コンパイル時間 733 ms
コンパイル使用メモリ 82,060 KB
実行使用メモリ 107,540 KB
最終ジャッジ日時 2024-10-06 04:48:30
合計ジャッジ時間 5,624 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

'''
    Python3(PyPy3) Template for Programming-Contest.

    author : sgsw

    generated : 2021/08/14   
    when : 23:49:30

'''


def input():
    return sys.stdin.readline().rstrip()


DXY = [(0, -1), (1, 0), (0, 1), (-1, 0)]  # LDRU
mod = 998244353
inf = 1 << 64

def main():
    n = int(input())
    a = list(map(int, input().split()))
    if sorted(a) == a:
        print(0)
    else:
        #case 1
        is_sorted = [False]*(n)
        for i in reversed(range(0, n)):
            if i == n - 1:
                is_sorted[i] = True
            else:
                if is_sorted[i + 1] == True and (a[i] <= a[i + 1]):
                    is_sorted[i] = True

        dp = [0]*(n)
        maxv = a.copy()

        for l in range(1, n):
            maxv[l] = max(maxv[l - 1], maxv[l])
            if l == 1:
                dp[l] = (not a[0] <= a[1]) + (not a[1] <= a[0])
            else:
                dp[l] = (dp[l - 1] - (not a[l - 1] <= a[0])) + \
                    (not a[l - 1] <= a[l]) + (not a[l] <= a[0])

        for i in range(1, n):
            if i != n - 1:
                if maxv[i] <= a[i + 1] and is_sorted[i + 1] and dp[i] == 1:
                    print(1)
                    return
            else:
                if dp[i] == 1:
                    print(1)
                    return
        print(2)

    return 0


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