No Rules Rules

회전하는 큐 (feat. 백준, 1021번) 본문

생활/코테

회전하는 큐 (feat. 백준, 1021번)

개발하는 완두콩 2022. 8. 13. 17:50
728x90
반응형

회전하는 큐
https://www.acmicpc.net/problem/1021

 

1021번: 회전하는 큐

첫째 줄에 큐의 크기 N과 뽑아내려고 하는 수의 개수 M이 주어진다. N은 50보다 작거나 같은 자연수이고, M은 N보다 작거나 같은 자연수이다. 둘째 줄에는 지민이가 뽑아내려고 하는 수의 위치가

www.acmicpc.net

 

반응형

 

// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <algorithm>
#include <deque>
using namespace std;
int main() {
	ios::sync_with_stdio(false), cin.tie(NULL);
	register int N, M, total = 0;
	deque<int> ans;
	cin >> N >> M;
	for (register int n = 1; n <= N; ++n)
		ans.push_back(n);
	for (register int m = 0, v, pos1, pos2; m < M; ++m) {
		cin >> v;
		pos1 = distance(ans.begin(), find(ans.begin(), ans.end(), v));
		pos2 = ans.size() - pos1;
		total += min(pos1, pos2);
		if (pos1 <= pos2) {
			while (pos1--)
				ans.push_back(ans.front()), ans.pop_front();
			ans.pop_front();
		}
		else {
			while (pos2--)
				ans.push_front(ans.back()), ans.pop_back();
			ans.pop_front();
		}
	}
	cout << total;
	return 0;
}
// *&)*@*
  1. 문제로 주어진 뽑아야할 번호에 대해서 앞에서 빼는 경우, 뒤에서 빼는 경우의 최소 횟수를 카운팅하는 문제입니다.
  2. 문제의 3가지 연산중 첫번째는 '항상 pop_front한다' 라는 것입니다.
728x90
반응형

'생활 > 코테' 카테고리의 다른 글

색종이 만들기 (feat. 백준, 2630번)  (0) 2022.08.13
AC (feat. 백준, 5430번)  (0) 2022.08.13
덱 (feat. 백준, 10866번)  (0) 2022.08.13
프린터 큐 (feat. 백준, 1966번)  (0) 2022.08.12
요세푸스 문제 0 (feat. 백준, 11866번)  (0) 2022.08.12
Comments