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()