※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 26043번 문제인 식당 메뉴이다.
문제는 아래 링크를 확인하자.

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

 

26043번: 식당 메뉴

첫 번째 1 2 1을 처리한 후, 대기 줄은 (2 1)이 된다. (2 1)에서 2는 학생 번호, 1은 좋아하는 메뉴 번호를 나타낸다. 두 번째 1 1 1을 처리한 후, 대기 줄은 (2 1) (1 1)이 된다. (1 1)에서 앞쪽 1은 학생 번호

www.acmicpc.net

문제의 상황은 큐(queue) 자료구조를 이용해 구현해낼 수 있다. pair<int, int>형을 담는 큐를 이용해 학생번호와 그 학생이 선호나는 메뉴번호를 함께 저장해 시뮬레이션을 돌려 문제를 해결하자.

 

각 목록의 학생 번호를 출력할 때 번호를 오름차순으로 출력해야 한다는 조건에 유의하자.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

int N;
queue<pair<int,int>> que;

vector<int> A, B, C;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N;

	for (int i = 0; i < N; i++) {
		int q; cin >> q;
		if (q == 1) {
			int a, b; cin >> a >> b;
			que.push(make_pair(a, b));
		}
		else {
			int b; cin >> b;
			if (b == que.front().second) A.emplace_back(que.front().first);
			else B.emplace_back(que.front().first);
			que.pop();
		}
	}

	while (!que.empty()) {
		C.emplace_back(que.front().first);
		que.pop();
	}

	sort(A.begin(), A.end());
	sort(B.begin(), B.end());
	sort(C.begin(), C.end());

	if (A.empty()) cout << "None\n";
	else {
		for (auto& x : A) cout << x << ' ';
		cout << '\n';
	}

	if (B.empty()) cout << "None\n";
	else {
		for (auto& x : B) cout << x << ' ';
		cout << '\n';
	}

	if (C.empty()) cout << "None\n";
	else {
		for (auto& x : C) cout << x << ' ';
		cout << '\n';
	}
}
728x90

+ Recent posts