끔찍한 문제 만남
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StackSort{
public static void main(String[] args) throws Exception {
// Scanner 객체보다 빠르게 하기 위해 사용. 한 줄을 통째로 입력받는다.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder(); // String 연산 시간을 줄이기 위해 사용
int n = Integer.parseInt(br.readLine()); // 입력할 숫자 갯수
int count; // 입력한 값
int max = 0; // stack안에서 제일 큰 값
int top = 0; // stack에서 최상단에 있는 값
int[] stack = new int[n];
while(n-- > 0){ // 입력받은 값의 수가 0보다 클 때 까지
count = Integer.parseInt(br.readLine());
if(count > max){
// 스택에 값이 없을 경우
for(int i=max+1; i<=count; i++){
stack[top++] = i;
sb.append("+\n"); // push
}
max = count;
}else if(stack[top-1] != count) { // 종료조건을 확인하기 위해
System.out.println("NO");
return; // 아예 메소드를 종료시켜야 하기때문에 break을 쓰지 않는다.
}
// 무조건 한번은 pop을 하기 때문에
top--;
sb.append("-\n"); // pop
}
System.out.println(sb);
}
}
설명은 집 가서 쓰겠삼