손으로 그려보는 자료구조
연관문제 풀이
회전하는 큐 - 백준 1021번
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Qu {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int answer = 0;
String[] s = (br.readLine()).split(" ");
int size = Integer.parseInt(s[0]);
String[] targets = (br.readLine()).split(" ");
Integer[] qu = new Integer[size];
for (int i = 0; i < size; i++) {
qu[i] = i + 1;
}
List<Integer> queue = new ArrayList<>(Arrays.asList(qu));
int cur = 0;
int step = 0;
for (String target : targets) {
int t = Integer.parseInt(target);
while (queue.get(cur) != t) {
cur++;
step++;
if (cur == size) {
cur = 0;
}
}
queue.remove(cur);
if (step >= size - step) {
answer += size - step;
} else {
answer += step;
}
step = 0;
size--;
if (size == cur) {
cur = 0;
}
}
System.out.println(answer);
}
}