백준
[1021] 회전하는 큐
by Jcoder
2018. 7. 22.
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque<int> dq;
deque<int>::iterator iter;
int n, m, i, cnt = 0;
cin >> n >> m;
for (i = 1; i <= n; i++)
{
dq.push_back(i);
}
for (i = 0; i < m; i++)
{
int num;
cin >> num;
int index = 1;
for (iter = dq.begin(); iter < dq.end(); iter++)
{
if (*iter == num)
break;
index++;
}
int left = index - 1;
int right = dq.size() - index + 1;
if (left < right)
{
for (int j = 1; j <= left; j++)
{
dq.push_back(dq.front());
dq.pop_front();
cnt++;
}
dq.pop_front();
}
else
{
for (int j = 1; j <= right; j++)
{
dq.push_front(dq.back());
dq.pop_back();
cnt++;
}
dq.pop_front();
}
}
cout << cnt << endl;
return 0;
}