백준 2346: 풍선 터뜨리기 [C++]

cozups·2022년 5월 18일
0

보자마자 원형 큐인 것을 알았다.

문제는... 원형 큐를 어떻게 구현할 것인가..?

vector를 이용해야하나 생각했는데 deque으로 푸는 문제였다.

#include <iostream>
#include <deque>

using namespace std;

int main() {
	deque<pair<int, int>> q;
	int N;

	cin >> N;

	for (int i = 1; i <= N; i++) {
		int temp;
		cin >> temp;
		q.push_back({ i, temp });
	}

	while (!q.empty()) {
		int next = q.front().second;
		cout << q.front().first << ' ';
		q.pop_front();

		if (q.empty()) break;

		if (next > 0) {
			for (int i = 0; i < next-1; i++) {
				q.push_back(q.front());
				q.pop_front();
			}
		}
		else {
			next *= -1;
			for (int i = 0; i < next; i++) {
				q.push_front(q.back());
				q.pop_back();
			}
		}
	}

	return 0;
}
profile
이제는 더 이상 물러날 곳이 없다

0개의 댓글