목차

Y-수열

ps
링크acmicpc.net/…
출처BOJ
문제 번호20127
문제명Y-수열
레벨골드 5
분류

애드혹

시간복잡도O(n)
인풋사이즈n<=1,000,000
사용한 언어Python 3.11
제출기록41148KB / 84ms
최고기록84ms
해결날짜2023/08/28

풀이

코드

"""Solution code for "BOJ 20127. Y-수열".

- Problem link: https://www.acmicpc.net/problem/20127
- Solution link: http://www.teferi.net/ps/problems/boj/20127

Tags: [ad hoc]
"""

INF = float('inf')


def main():
    N = int(input())
    a = [int(x) for x in input().split()]

    dec_inds = [i for i, x, y in zip(range(N), a, a[1:]) if x > y]
    if not dec_inds:
        print('0')
        return
    answer_to_nondec = (
        dec_inds[0] if len(dec_inds) == 1 and a[0] >= a[-1] else INF
    )

    inc_inds = [i for i, x, y in zip(range(N), a, a[1:]) if x < y]
    if not inc_inds:
        print('0')
        return
    answer_to_noninc = (
        inc_inds[0] if len(inc_inds) == 1 and a[0] <= a[-1] else INF
    )
    answer = min(answer_to_nondec, answer_to_noninc)

    print('-1' if answer == INF else answer + 1)


if __name__ == '__main__':
    main()