반응형

17299번

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int[] Farr = new int[1000001];

		int N = Integer.parseInt(br.readLine());

		Stack<Integer> originStack = new Stack<Integer>();
		Stack<Integer> maxStack = new Stack<Integer>();
		Stack<Integer> NGFStack = new Stack<Integer>();

		StringTokenizer st = new StringTokenizer(br.readLine());

		for (int i = 0; i < N; i++) {
			int num = Integer.parseInt(st.nextToken());
			originStack.push(num);
			Farr[num]++;
		}

		while (!originStack.empty()) {

			int originNum = originStack.peek();

			if (maxStack.empty()) {
				NGFStack.push(-1);
				maxStack.push(originStack.pop());
			} else {

				if (Farr[originNum] < Farr[maxStack.peek()]) {
					NGFStack.push(maxStack.peek());
					maxStack.push(originStack.pop());
				} else {
					maxStack.pop();
				}

			}
		}

		while (!NGFStack.empty()) {
			bw.write(NGFStack.pop() + " ");
		}

		bw.flush();
		bw.close();
	}

}
반응형

'알고리즘 > 백준 문제[추후 옮길예정]' 카테고리의 다른 글

[JAVA] 백준 10820번  (0) 2021.05.11
[JAVA] 백준 10808번  (0) 2021.05.10
[JAVA] 백준 17298번  (0) 2021.05.10
[JAVA] 백준 10799번  (0) 2021.05.08
[JAVA] 백준 17413번  (0) 2021.05.07

+ Recent posts