#include <iostream> #include <list> #include <unordered_map> using namespace std; int main() { int n, m; cin >> n >> m; // 使用链表来存储队列元素,方便移动元素到最前面 list<int> queueList; for (int i = 1; i <= n; ++i) { queueList.push_back(i); } // 使用哈希表记录每个元素在链表中的位置迭代器,方便快速查找和移动 unordered_map<int, list<int>::iterator> posMap; for (auto it = queueList.begin(); it!= queueList.end(); ++it) { posMap[*it] = it; } for (int i = 0; i < m; ++i) { int x; cin >> x; // 获取要移到最前面的元素在链表中的位置迭代器 auto it = posMap[x]; // 将该元素从当前位置移除 queueList.erase(it); // 将该元素插入到链表头部 queueList.push_front(x); // 更新哈希表中该元素的位置迭代器 posMap[x] = queueList.begin(); } for (int num : queueList) { cout << num << " "; } cout << endl; return 0; } /************************************************************** Problem: 1527 User: linyifan Language: C++ Result: Accepted Time:424 ms Memory:9056 kb ****************************************************************/