[BOJ] 1021 회전하는 큐

Time Lapse : 31min 44sec

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
#include <iostream>
#include <vector>
using namespace std;
int main(void){
int M, N;
cin>>N>>M;
int require[M];
vector<int> rq(N);
for(int i=1;i<=N;i++)
rq[i-1]=i;

for(int i=0;i<M;i++)
cin>>require[i];
int answer = 0;
for(int i=0;i<M;i++){
for(int j=0;j<rq.size();j++){
if(rq[j]==require[i]){
answer += j<rq.size()-j ? j:rq.size()-j;
vector<int> new_rq(N-i-1);
int idx = j+1;
for(int k=0;k<rq.size()-1;k++){
if(idx==rq.size())
idx=0;
new_rq[k]=rq[idx++];
}
rq.assign(new_rq.begin(),new_rq.end());
break;
}
}
}
cout<<answer<<endl;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/1021/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.