importsysN=int(sys.stdin.readline().rstrip())Numbers=[iforiinrange(1,N+1)]Sequence=[]foriinrange(N):v=int(sys.stdin.readline().rstrip())Sequence.append(v)result=""stack=[]ans=[]foriinrange(N):ifstackandstack[-1]==Sequence[i]:ans.append(stack.pop())result+="-"elifnotNumbers:whilestack:ans.append(stack.pop())breakelifNumbers[0]<=Sequence[i]:try:whileNumbers[0]<=Sequence[i]:stack.append(Numbers.pop(0))result+="+"ans.append(stack.pop())result+="-"exceptIndexError:ifstack[-1]==Sequence[i]:ans.append(stack.pop())result+="-"continueifans==Sequence:foriinresult:print(i)else:print("NO")# 뭔가 좀 지저분해보이지만..# 입력받은 수열의 요소 마다 검증을 하고 싶었고# 각 요소마다 진행사항을 등록하고 싶었다. # 예를 들어) 수열의 첫 번재 '4'가 나오기 위해선. # 1,2,3,4가 스택에 들어갔다가 마지막 4가 출력되면 된다.# 이런 과정을 코딩하고 싶었다. # 인터넷에 있는 까리한 코딩과 비교해봤는데.# 다행이 내 코드가 속도면에서 우위를 점했다.# 메모리는 졌음. ㅎ
importsysN=int(sys.stdin.readline().rstrip())arr=list(map(int,sys.stdin.readline().split()))NGE=[-1]*Nstack=[0]i=1whilestackandi<N:whilestackandarr[stack[-1]]<arr[i]:NGE[stack[-1]]=arr[i]stack.pop()stack.append(i)i+=1print(*NGE)# stack에 arr의 인덱스를 저장(비교 기준 설정)# i로 비교할 인덱스를 지정해 비교# stack에 0 저장# i = 1 즉, arr[1] = 5를 stack에 저장되어있는# 인덱스값(arr[0])과 비교 NGE라면 arr[i]을 NGE에 저장 # 이게 왜 빠른건지 잘 모르겠다.