top1编程
← 返回题目
题解

【基础】会话列表

1 条题解

  • 0
    @ 2026-7-28 22:09:33
    #include <iostream>
    #include <queue>
    using namespace std;
    
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            int N;
            cin >> N;
            queue<long long> q;
            
            for (int i = 0; i < N; i++)
            {
                long long x;
                cin >> x;
                queue<long long> temp;
                bool has = false;
                
                // 遍历原有队列,移除x
                while (!q.empty())
                {
                    long long num = q.front();
                    q.pop();
                    if (num == x)
                    {
                        has = true;
                    }
                    else
                    {
                        temp.push(num);
                    }
                }
                // 当前消息放到列表最上方
                q.push(x);
                // 其余元素依次接上
                while (!temp.empty())
                {
                    q.push(temp.front());
                    temp.pop();
                }
            }
            
            // 输出结果
            bool first = true;
            while (!q.empty())
            {
                if (!first)
                    cout << " ";
                first = false;
                cout << q.front();
                q.pop();
            }
            cout << endl;
        }
        return 0;
    }
    
    • 1