반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class BJ_G5_2493_탑 {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringBuffer output = new StringBuffer();
static StringTokenizer tokens;
static int N;
public static void main(String[] args) throws NumberFormatException, IOException {
N = Integer.parseInt(input.readLine());
tokens = new StringTokenizer(input.readLine());
Stack<Tower> stack = new Stack<Tower>();
for (int n = 1; n <= N; n++) {
int num = Integer.parseInt(tokens.nextToken());
while(!stack.isEmpty()) {
if(stack.peek().height >= num) {
output.append(stack.peek().pos + " ");
break;
}
stack.pop();
}
if(stack.isEmpty()) output.append("0 ");
stack.push(new Tower(num, n));
}
//System.out.println(stack);//입력확인
System.out.println(output);
//
}
static class Tower{
int height;
int pos;
public Tower(int height, int pos) {
this.height = height;
this.pos = pos;
}
}
}
반응형
'코딩테스트 > 백준문제' 카테고리의 다른 글
[Java] 백준_1244_스위치켜고끄기 (0) | 2021.02.08 |
---|---|
[Java] 백준_15649_N과M(1) (0) | 2021.02.08 |
[python 파이썬] 백준 1920번 : 수 찾기 (0) | 2020.11.29 |
[python 파이썬] 백준 10039번 : 평균 점수 (0) | 2020.11.09 |
[python 파이썬] 백준 2839번 : 설탕배달 (0) | 2020.10.21 |