# 題目: UVa 12207 - That is Your Queue

# 題目說明

政府終於解決了全民健保問題,而建立了一套系統
你需要用程式寫這個系統,說明如下:

  • 每個人被分配到一個編號,由1開始排
  • 當緊急狀況發生時,會有人優先移到前面

INPUT:
每筆測資第一行有兩個整數PCP代表國家的人口數,C代表接下來的指令數
(當PC0時結束程式)
接下來會有C行,每行會有NE x

  • N代表輸出最前面的人並移動到最後
  • E x代表將編號為x的人移動到最前面

OUTPUT:
每遇到N,輸出最前面的人

# 解題方法

利用deque儲存資料,方便進行push_backpush_frontpop_front
注意pushpop的順序即可

# 參考程式碼

#include <iostream>
#include <deque>
#include <algorithm>

using namespace std;

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

	int P, C, count = 1;

	while (cin >> P >> C && P != 0 && C != 0) 
	{
		
		deque<int> line;
		char state;
		int s;

		for (int i = 1; i <= min(P, C); i++) line.emplace_back(i);

		cout << "Case " << count++ << ":\n";
		while (C-- && cin >> state) 
		{
			if (state == 'N') 
			{
				cout << line.front() << "\n";
				line.emplace_back(line.front());
				line.pop_front();
			}
			else 
			{
				cin >> s;
				for (auto it = line.begin(); it != line.end(); it++)
					if (*it == s) 
					{
						line.erase(it);
						break;
					}
				line.emplace_front(s);
			}
		}
	}

	return 0;
}

# 參考資料

https://www.larrysprognotes.com/UVa%20-%2012207/