-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1021.cpp
62 lines (50 loc) · 1.22 KB
/
1021.cpp
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
#include<iostream>
#include<cstdio>
#include<deque>
using namespace std;
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio();
int N, M;
cin >> N >> M;
deque<int> dq;
for(int i=1; i<=N; i++) dq.push_back(i);
int cnt = 0;
for(int i=0; i<M; i++){
int target;
int targetIndex;
cin >> target;
for(int j=0; j<dq.size(); j++){
if(dq[j] == target){
targetIndex = j;
break;
}
}
if(targetIndex*2 <= dq.size()){
while(true){
if(dq.front() == target){
dq.pop_front();
break;
}
else{
cnt++;
dq.push_back(dq.front());
dq.pop_front();
}
}
}else{
while(true){
if(dq.front() == target){
dq.pop_front();
break;
}
else{
cnt++;
dq.push_front(dq.back());
dq.pop_back();
}
}
}
}
cout << cnt;
}