크기가 N인 수열 A = A1, A2, ..., AN이 있다. 수열의 각 원소 Ai에 대해서 오큰수 NGE(i)를 구하려고 한다. Ai의 오큰수는 오른쪽에 있으면서 Ai보다 큰 수 중에서 가장 왼쪽에 있는 수를 의미한다. 그러한 수가 없는 경우에 오큰수는 -1이다.
예를 들어, A = [3, 5, 2, 7]인 경우 NGE(1) = 5, NGE(2) = 7, NGE(3) = 7, NGE(4) = -1이다. A = [9, 5, 4, 8]인 경우에는 NGE(1) = -1, NGE(2) = 8, NGE(3) = 8, NGE(4) = -1이다.
첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에 수열 A의 원소 A1, A2, ..., AN (1 ≤ Ai ≤ 1,000,000)이 주어진다.
총 N개의 수 NGE(1), NGE(2), ..., NGE(N)을 공백으로 구분해 출력한다.
import java.io.*;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Stack<Integer> stack = new Stack<>();
Stack<Integer> tmpStack = new Stack<>();
String[] str = br.readLine().split(" ");
StringBuilder sb = new StringBuilder();
for (int i = N-1; i >= 0; i--) {
int curN = Integer.parseInt(str[i]);
while (!tmpStack.isEmpty()) {
if (tmpStack.peek() > curN) {
stack.push(tmpStack.peek());
break;
} else {
tmpStack.pop();
}
}
if (tmpStack.isEmpty()) {
stack.push(-1);
}
tmpStack.push(curN);
}
while(!stack.isEmpty())
sb.append(stack.pop()).append(" ");
System.out.println(sb);
}
}