-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1021 회전하는 큐.js
102 lines (98 loc) · 2.39 KB
/
1021 회전하는 큐.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class Deque {
constructor() {
this.arr = [];
this.head = 0;
this.tail = 0;
}
push_front(item) {
if (this.arr[0]) {
for (let i = this.arr.length; i > 0; i--) {
this.arr[i] = this.arr[i - 1];
}
}
this.arr[this.head] = item;
this.tail++;
}
push_back(item) {
this.arr[this.tail++] = item;
}
top() {
if (this.head >= this.tail) {
return null;
} else {
const result = this.arr[this.head];
return result;
}
}
where(item) {
if (this.head >= this.tail) {
return null;
} else {
const newArr = this.arr.slice(this.head, this.tail);
return newArr.indexOf(item);
}
}
size() {
if (this.head >= this.tail) {
return null;
} else {
const newArr = this.arr.slice(this.head, this.tail);
return newArr.length;
}
}
pop_front() {
if (this.head >= this.tail) {
return null;
} else {
const result = this.arr[this.head++];
return result;
}
}
pop_back() {
if (this.head >= this.tail) {
return null;
} else {
const result = this.arr[--this.tail];
return result;
}
}
}
const fs = require("fs");
let input = fs
.readFileSync("./dev/stdin")
.toString()
.trim()
.split("\n")
.map((e) => e.split(" ").map((v) => +v));
let spin = new Deque();
let [N, M] = input[0];
for (let i = 1; i <= N; i++) {
spin.push_back(i);
}
let idx = 0;
let count = 0;
let mCount = 0;
while (mCount < M) {
if (spin.top() === input[1][idx]) {
spin.pop_front();
mCount++;
idx++;
} else {
if (spin.where(input[1][idx]) < spin.size() / 2) {
while (spin.top() !== input[1][idx]) {
spin.push_back(spin.pop_front());
count++;
}
} else {
while (spin.top() !== input[1][idx]) {
spin.push_front(spin.pop_back());
count++;
}
}
}
}
// spin.push_back(1);
// spin.push_back(2);
// spin.pop_front();
// console.log(spin.arr);
console.log(count);