-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon1021.java
47 lines (41 loc) · 1.29 KB
/
Baekjoon1021.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package Algorithms;
import java.util.LinkedList;
import java.util.Scanner;
/**
* https://www.acmicpc.net/problem/1021
*/
public class Baekjoon1021 {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int N = sc.nextInt();
int M = sc.nextInt();
int[] answer = new int[M];
int[] arr = new int[M];
LinkedList<Integer> deque = new LinkedList<>();
for (int i = 1; i <= N; i++) {
deque.add(i);
}
for (int i = 0; i < M; i++) {
answer[i] = sc.nextInt();
}
int cnt = 0;
int index = 0;
while (arr[M - 1] == 0) {
if (deque.peekFirst() == answer[index]) {
arr[index] = deque.removeFirst();
index++;
} else {
int position = deque.indexOf(Integer.valueOf(answer[index]));
// 오른쪽으로 이동이 유리함.
if (position < deque.size() + 1 - position) {
deque.offerLast(deque.pollFirst());
} else {
//왼쪽으로 이동이 유리
deque.offerFirst(deque.pollLast());
}
cnt++;
}
}
System.out.println(cnt);
}
}