티스토리 뷰

https://www.acmicpc.net/problem/1021

 

1021번: 회전하는 큐

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

www.acmicpc.net

어제부터 다시 시작한 하루 필수 1문제를 시작했습니다. 확실히 오랜만에 푸는거라 머리가 제대로 안돌아가더라구요. 

이 문제는 인덱스를 구해서 이분법을 통해 풀었습니다. 중앙 보다 작은 인덱스면 2번, 크면 3번이 작동하도록 했습니다.

 

 

 

코드

#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cout.tie(nullptr);

	int N, M;
	int input;
	cin >> N >> M;

	deque<int> t1;
	queue<int> t2;
	for (int i = 1; i <= N; i++) {
		t1.push_back(i);
	}
	for (int i = 0; i < M; i++) {
		cin >> input;
		t2.push(input);
	}
	int count = 0;
	
	while (!t2.empty()) {
		int Index = 0;
		for (int i : t1) {
			if (i == t2.front())
				break;
			Index++;
		}
		if (Index > t1.size()/2) {
			while (1) {
				if (t1.front() == t2.front()) {
					t1.pop_front();
					t2.pop();
					break;
				}
				t1.push_front(t1.back());
				t1.pop_back();
				count++;
			}
		}
		else {
			while (1) {
				if (t1.front() == t2.front()) {
					t1.pop_front();
					t2.pop();
					break;
				}
				t1.push_back(t1.front());
				t1.pop_front();
				count++;
			}
		}
	}	
	cout << count << "\n";
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함