-
Notifications
You must be signed in to change notification settings - Fork 0
/
1021(Deque).java
53 lines (46 loc) · 1.61 KB
/
1021(Deque).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
48
49
50
51
52
53
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Hororop {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
LinkedList<Integer> deque = new LinkedList<Integer>();
for(int i = 1; i < N+1; i++){
deque.add(i);
}
int arr[] = new int[M];
st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i < M; i++){
arr[i] = Integer.parseInt(st.nextToken());
}
int count = 0;
for(int i = 0; i < M; i++){
int target = deque.indexOf(arr[i]); //뽑고싶은수가 덱에서 어디에?
int half = 0;
if(deque.size() % 2 == 0){
half = deque.size() / 2 - 1;
}else{
half = deque.size() / 2;
}
if(target <= half){
for(int j = 0; j < target; j++){
int temp = deque.removeFirst();
deque.addLast(temp);
count++;
}
}else{
for(int j = deque.size(); j > target; j--){
int temp = deque.removeLast();
deque.addFirst(temp);
count++;
}
}
deque.removeFirst();
}
System.out.println(count);
}
}