-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekJoon1021.cpp
More file actions
70 lines (60 loc) · 1.22 KB
/
BaekJoon1021.cpp
File metadata and controls
70 lines (60 loc) · 1.22 KB
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
//BaekJoon 1021 회전하는 큐
#include <iostream>
#include <string>
#include <deque>
using namespace std;
deque<int> dq;
deque<int>::iterator iter;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
//get inputs
int n, m, i; cin >> n >> m;
int cnt = 0;
//initialize
for (i = 1; i <= n; i++) {
dq.push_back(i);
}
//pop elements
for(i = 0 ; i < m ; i++) {
//get element
int element; cin >> element;
int size = dq.size();
//element position
int idx = 1;
//check where element is placed
for (iter = dq.begin(); iter < dq.end(); iter++) {
if (*iter == element) break;
idx++;
}
//calculate left or right
//+1 to right, bcz have to pop_front
int left = idx - 1;
int right = size - idx + 1;
//move left
if (left < right) {
//pop front and push it to back
for (int j = 0; j < left; j++) {
dq.push_back(dq.at(0));
dq.pop_front();
cnt++;
}
//pop element
dq.pop_front();
}
//move right
else {
//pop back and push it to front
for (int j = 0; j < right; j++) {
dq.push_front(dq.at(size - 1));
dq.pop_back();
cnt++;
}
//pop element
//bcz of condition, pop_back now allowed
dq.pop_front();
}
}
cout << cnt << "\n";
return 0;
}