하다보니

1021번-회전하는 큐 본문

알고리즘 풀이/백준

1021번-회전하는 큐

claire 2022. 1. 23. 13:04
#include<bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	deque<int> Q;
	int n, m;
	int ans = 0;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) Q.push_back(i);
	while (m--) {
		int a;
		cin >> a;
		int idx = find(Q.begin(), Q.end(), a)-Q.begin();
		while (Q.front() != a) {
			if (idx<( Q.size() - idx)) {
				Q.push_back(Q.front());
				Q.pop_front();
				ans++;
			}
			else {
				Q.push_front(Q.back());
				Q.pop_back();
				ans++;
			}
		}
		Q.pop_front();
	}
	cout << ans;
}

'알고리즘 풀이 > 백준' 카테고리의 다른 글

2178번-미로탐색  (0) 2022.01.29
1926번-그림  (0) 2022.01.26
10866번-덱  (0) 2022.01.23
2164번-카드2  (0) 2022.01.23
10845번-큐  (0) 2022.01.23