top1编程
← 返回题目
题解

[JLOI2011] 不重复数字

1 条题解

  • 0
    @ 2026-10-21 1:10:05

    1. 解题思路

    按原顺序读数,集合首次插入成功时立即输出该数,否则跳过。

    2. 坑点和注意事项

    T≤50、n≤5×10^4,数为32位有符号整数。保留首次出现顺序,不能排序后直接输出;用set保证最坏O(log n)判重,避免按值开巨大数组。

    3. 复杂度

    • 时间复杂度:O(Tn log n)。
    • 空间复杂度:O(n)。
    /*
    思路:按原顺序读数,集合首次插入成功时立即输出该数,否则跳过。
    关键规则与坑点:T≤50、n≤5×10^4,数为32位有符号整数。保留首次出现顺序,不能排序后直接输出;用set保证最坏O(log n)判重,避免按值开巨大数组。
    时间复杂度:O(Tn log n)。空间复杂度:O(n)。
    */
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int t;
        cin >> t;
        while (t--)
        {
            int n;
            cin >> n;
            set<int> seen;
            bool first = 1;
            while (n--)
            {
                int x;
                cin >> x;
                if (seen.insert(x).second)
                {
                    if (!first)
                        cout << ' ';
                    cout << x;
                    first = 0;
                }
            }
            cout << '\n';
        }
    }
    
    • 1