n = int(input())
a = list(map(int, input().split(' ')))
a += a
s = [0]
sc = [0]
ans = 0
for i in range(2*n):
if a[i] <= s[-1]:
s += [a[i]]
sc += [1]
continue
last = sc[-1]
while len(s) and a[i] > s[-1]:
s.pop(-1)
last = max(last, sc.pop(-1))
ans = max(ans, last + 1)
s += [a[i]]
sc += [last + 1]
print(ans)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | n = int(input()) a = list(map(int, input().split(' '))) a += a s = [0] sc = [0] ans = 0 for i in range(2*n): if a[i] <= s[-1]: s += [a[i]] sc += [1] continue last = sc[-1] while len(s) and a[i] > s[-1]: s.pop(-1) last = max(last, sc.pop(-1)) ans = max(ans, last + 1) s += [a[i]] sc += [last + 1] print(ans) |
English