백준 2346번
https://www.acmicpc.net/problem/2346
Deque 문제이다.
그냥 LinkedList 쓰면 바로 메모리 초과 발생함.
import java.io.*;
import java.util.*;
public class Main {
// input
private static BufferedReader br;
// variables
private static int N;
private static int[] arr;
private static class Balloon {
int num;
int idx;
private Balloon(int num, int idx) {
this.num = num;
this.idx = idx;
}
} // End of Balloon class
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
input();
bw.write(solve());
bw.close();
} // End of main()
private static String solve() {
StringBuilder sb = new StringBuilder();
sb.append(1).append(' ');
int move = arr[0];
ArrayDeque<Balloon> list = new ArrayDeque<>();
for (int i = 1; i < N; i++) {
list.offer(new Balloon(arr[i], i + 1));
}
while (!list.isEmpty()) {
if (move > 0) {
for (int i = 0; i < move - 1; i++) {
list.offer(list.poll());
}
Balloon cur = list.poll();
move = cur.num;
sb.append(cur.idx).append(' ');
} else {
for (int i = 0; i < -move - 1; i++) {
list.offerFirst(list.pollLast());
}
Balloon cur = list.pollLast();
move = cur.num;
sb.append(cur.idx).append(' ');
}
}
return sb.toString();
} // End of solve()
private static void input() throws IOException {
N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
} // End of input()
} // End of Main class