n=int(input()) a=list(map(int,input().split())) la=list(set(a)) la.sort() pa={x:i for i,x in enumerate(la)} a=[pa[x] for x in a] from collections import Counter from bisect import bisect_left as bl,bisect_right as br a.append(len(la)) ca=dict(Counter(a)) aa=a+a ary=[[] for _ in range(len(ca))] for i,x in enumerate(aa): ary[x].append(i) # 0-indexed binary indexed tree class BIT: def __init__(self, n): self.n = n self.data = [0]*(n+1) self.el = [0]*(n+1) # sum of [0,i) sum(a[:i]) def sum(self, i): if i==0:return 0 s = 0 while i > 0: s += self.data[i] i -= i & -i return s def add(self, i, x): i+=1 self.el[i] += x while i <= self.n: self.data[i] += x i += i & -i # sum of [l,r) sum(a[l:r]) def sumlr(self, i, j): return self.sum(j) - self.sum(i) # a[i] def get(self,i): i+=1 return self.el[i] bit=BIT(2*n*2) for i in range(2*n+2):bit.add(i,1) now=2*n+1 v=len(la) ans=0 bit.add(now,-1) bit.add(now-n-1,-1) for _ in range(n): ca[v]-=1 if ca[v]==0: v-=1 # nowより左のidxの中で最も右のvを探す idx=br(ary[v],now)-1 if bit.sumlr(ary[v][idx]+1,now): ans+=1 now=ary[v][idx] if now