https://www.acmicpc.net/problem/1138
1138번: 한 줄로 서기
첫째 줄에 사람의 수 N이 주어진다. N은 10보다 작거나 같은 자연수이다. 둘째 줄에는 키가 1인 사람부터 차례대로 자기보다 키가 큰 사람이 왼쪽에 몇 명이 있었는지 주어진다. i번째 수는 0보다
www.acmicpc.net
N이 10 이하로 매우 작기 때문에 아이디어로만 풀 수 있는 문제
자신의 왼쪽에 자기보다 큰 수를 저장해 놓은 배열 left_tall
순서를 저장하고 -1로 초기화해 놓은 배열 order 생성
left_tall의 순서가 오름차순으로 이미 정렬되어 있기 때문에 앞에서부터 차례대로 채워 넣기만 하면 된다.
public class Main {
public static int N, count;
public static int[] left_tall, order;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
N = Integer.parseInt(br.readLine());
left_tall = new int[N];
order = new int[N];
st = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++){
left_tall[i] = Integer.parseInt(st.nextToken());
order[i] = -1;
}
order[left_tall[0]] = 1;
for(int i=1; i<N; i++){
count=0;
for(int j=0; j<N; j++){
if(count == left_tall[i] && order[j] == -1) {order[j] = i+1; break;}
if(order[j] == -1) count++;
}
}
for(int i=0; i<N; i++) {
System.out.print(order[i] + " ");
}
}
}
'알고리즘 문제 > 구현' 카테고리의 다른 글
[Java] 구현 (쿠키의 신체 측정) (1) | 2023.03.03 |
---|---|
구현 (마법사 상어와 토네이도) (0) | 2022.07.01 |
구현 (누울 자리를 찾아라) (0) | 2022.04.27 |